I dont know why this code is not working, its not the actual code, but i think its rather the connections between the different layers MVC since the error gives a null-pointer exception. I am not very experienced in this but the Controller should know Model and View, the View and Model should only know the Controller and the listeners need to know the Controller and View too, right? And by "knowing" i mean that for example the Controller need to have private View v; and private Model m; as variables and in the Contructor you say this.v = v; and this.m = m; and also in its constructor do (Model m, View v) as parameters.
I think its somewhere here in the controller, view or model i messed up but i cant see it, if its really that problem.
I use 3 combo dropdownlists, one Shape, Color and Size and then a button for drawing it.
Here are the code and the line that is messing up is this.c.setShape(s); in the listener which should work if everything else was properly connected i guess.
Controller
package controller;
import java.awt.Color;
import view.View;
import model.*;
public class Controller
{
private Model m;
private View v;
public Controller(Model m, View v)
{
this.m = m;
this.v = v;
}
public void setShape(String s)
{
this.m.setShape(s);
}
public void setSize(int[] size)
{
m.setSize(size);
}
public void setColor(Color color) {
m.setColor(color);
}
}
View
package view;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import controller.*;
import model.*;
public class View extends javax.swing.JFrame
{
private static final long serialVersionUID = 1L;
private Controller c;
private JFrame jf;
private JComboBox shape;
private JComboBox color;
private JComboBox size;
private JButton draw;
private DrawingContainer dc;
private DrawingUtil du;
private DrawingPanel dp;
public View()
{
this.c = c;
initComponents();
}
private void initComponents()
{
this.jf = new JFrame();
this.dc = new DrawingContainer();
this.du = new DrawingUtil();
this.dp = new DrawingPanel(dc);
this.shape = new JComboBox();
this.shape.addItem("Choose Shape");
this.shape.addItem("Circle");
this.shape.addItem("Rectangle");
this.shape.addItem("Line");
this.shape.addActionListener(new ShapeListener(this.c, this));
this.color = new JComboBox();
this.color.addItem("Choose color");
this.color.addItem("Red");
this.color.addItem("Green");
this.color.addItem("Blue");
this.color.addActionListener(new ColorListener(this.c, this));
this.size = new JComboBox();
this.size.addItem("Choose size");
this.size.addItem("1");
this.size.addItem("3");
this.size.addItem("5");
this.size.addActionListener(new SizeListener(this.c, this));
this.draw = new JButton("Draw");
this.draw.addActionListener(new DrawListener());
this.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
this.setTitle("DrawingGUI");
int width = 350;
int height = 500;
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
int x = (screen.width - width) / 2;
int y = (screen.height - height) / 2;
this.jf.setBounds(x, y, width, height);
this.jf.add(dp);
this.jf.setSize(500, 500);
this.dp.setBackground(Color.WHITE);
this.dp.add(shape);
this.dp.add(color);
this.dp.add(size);
this.dp.add(draw);
this.pack();
this.jf.setResizable(false);
this.jf.setVisible(true);
}
public void setShape(JComboBox shape)
{
this.shape = shape;
}
public JComboBox getShape()
{
return shape;
}
public void setColor(JComboBox color)
{
this.color = color;
}
public JComboBox getColor()
{
return color;
}
public void setSize(JComboBox size)
{
this.size = size;
}
public JComboBox getSizez()
{
return size;
}
public static void main(String[] args)
{
new View();
}
}
Model
package model;
import java.awt.Color;
import controller.Controller;
public class Model
{
private String shape;
private Color color;
private int[] size;
private Controller c;
public Model(Controller c)
{
this.c = c;
}
public void setShape(String s)
{
this.shape = s;
}
public String getShape()
{
return shape;
}
public void setColor(Color color)
{
this.color = color;
}
public Color getColor()
{
return color;
}
public void setSize(int[] size)
{
this.size = size;
}
public int[] getSize()
{
return size;
}
}
LISTENER
package view;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
import controller.Controller;
public class ShapeListener implements ActionListener
{
private View v;
private Controller c;
public ShapeListener(View v, Controller c)
{
this.v = v;
this.c = c;
}
public void actionPerformed(ActionEvent e)
{
if(this.v.getShape().getSelectedItem().equals("Circle") || this.v.getShape().getSelectedItem().equals("Rectangle") || this.v.getShape().getSelectedItem().equals("Line"))
{
String s = this.v.getShape().getSelectedItem().toString();
[B]this.c.setShape(s);[/B]
}
else
{
JOptionPane.showMessageDialog(null, "You need to select a Shape");
}
}
}
Error message
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at view.ShapeListener.actionPerformed(ShapeListener.java:24)
at javax.swing.JComboBox.fireActionEvent(JComboBox.java:1197)
at javax.swing.JComboBox.setSelectedItem(JComboBox.java:561)
at javax.swing.JComboBox.setSelectedIndex(JComboBox.java:597)
at javax.swing.plaf.basic.BasicComboPopup$Handler.mouseReleased(BasicComboPopup.java:808)
at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:232)
at java.awt.Component.processMouseEvent(Component.java:5517)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3135)
at javax.swing.plaf.basic.BasicComboPopup$1.processMouseEvent(BasicComboPopup.java:476)
at java.awt.Component.processEvent(Component.java:5282)
at java.awt.Container.processEvent(Container.java:1966)
at java.awt.Component.dispatchEventImpl(Component.java:3984)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Component.dispatchEvent(Component.java:3819)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
at java.awt.Container.dispatchEventImpl(Container.java:2010)
at java.awt.Window.dispatchEventImpl(Window.java:1791)
at java.awt.Component.dispatchEvent(Component.java:3819)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
Really thankful for help guys,
Manne


Sign In
Create Account


Back to top









