• Post Reply Bookmark Topic Watch Topic
  • New Topic

What did I do wrong?  RSS feed

 
O Joseph
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I'm trying to develop a paint application, i have done everything i can think of but i dont seem to be able to get d shapes displayed on the canvas and whenever i click on the shape buttons it gives me the following error:

"Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at MyPaintPackage.MyPaintButton$MyPaintButtonListener.actionPerformed(My
PaintButton.java:74)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:18
49)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.jav
a:2169)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel
.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258
)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonL
istener.java:234)
at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:2
31)
at java.awt.Component.processMouseEvent(Component.java:5488)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3126)
at java.awt.Component.processEvent(Component.java:5253)
at java.awt.Container.processEvent(Container.java:1966)
at java.awt.Component.dispatchEventImpl(Component.java:3955)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Component.dispatchEvent(Component.java:3803)
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:1774)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchTh
read.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
ad.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)
Press any key to continue..."

do you have any suggestions on how i can tackle this problem?
thank you.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The error message says that the exception occured on line 74 of My
PaintButton.java. This is in the actionPerformed() method of an anonymous inner class in the MyPaintButton class that is in the MyPaintPackage package. (Do you see how I extracted all this information from the error message?) It also indicates the complete call stack that lead to this exception. Many of the classes and methods in the call stack are from the Java API, so you should ignore those for now. So to start, you need to look at line 74 in MyPaintButton.java. Feel free to post this line of code. Better yet, post the whole actionPerformed() method so that we can have some context for what that one line is doing. Also post any additional code that will help us understand what the method is doing. For example, you should post any declarations for variables and classes that are used within this method.

Regards,

Layne

p.s. You said that you tried everything you can think of. So what exactly did you try? And what were the results? When you ask a question, please provide as much details as possible so that we can help you more quickly.
 
O Joseph
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot. Here are the details:-

In the PaintButton class i have;
_________________________________________________________________________
private class MyPaintButtonListener implements ActionListener
{

public void actionPerformed(ActionEvent ae)
{
if(ae.getSource() == square)
{
74:myPaintCanvas.setDrawMode(myPaintCanvas.mySquare);
}
________________________________________________________________________

and this is a summary of what the MyPaintCanvasClass looks like:

package MyPaintPackage;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.util.*;
import java.io.*;
import javax.swing.*;
import javax.swing.filechooser.*;
//import javax.swing.filechooser.FileFilter;
import javax.imageio.*;

public class MyPaintCanvas extends JPanel implements MouseListener, MouseMotionListener, Serializable
{

private File file;
JFileChooser fileChooser;
//FileExtension filter;


private Color defaultBGColour, defaultFGColour/*, canvasFGColour, canvasBGColour*/;

protected static int open_fn, save_fn;

private boolean myFillCheck;
protected final static int mySquare = 1, myOval = 2, myRoundRect = 3, myLine = 4, myFreeH = 6,
fillMySquare = 11, fillMyOval = 22, fillMyRoundRect = 33;
private int x1, y1, x2, y2, lineX1, lineX2, lineY1, lineY2, drawMode = 0;
protected static Vector squareV, ovalV, rRV, lineV, freeHV, vector, fileV,
fillMySquareV, fillMyOvalV, fillMyRoundRectV;

/**
* Method MyPaintCanvas
*
*
*/
public MyPaintCanvas()
{
// TODO: Add your code here


squareV = new Vector();
fillMySquareV = new Vector();
ovalV = new Vector();
fillMyOvalV = new Vector();
rRV = new Vector();
fillMyRoundRectV = new Vector();
lineV = new Vector();
freeHV = new Vector();
fileV = new Vector();


addMouseListener(this);
addMouseMotionListener(this);

defaultFGColour = Color.black;
defaultBGColour = Color.white;
setBackground(defaultBGColour);

repaint();

}//End of constructor


public void mouseClicked(MouseEvent me)
{

}

public void mouseEntered(MouseEvent me)
{
setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
}

public void mouseExited(MouseEvent me)
{
setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}

public void mousePressed(MouseEvent me)
{
x1 = lineX1 = lineX2 = me.getX();
y1 = lineY1 = lineY2 = me.getY();
}

public void mouseReleased(MouseEvent me)
{
if (drawMode == mySquare)
{
if (myFillCheck)
{
if(x1 > me.getX() || y1 > me.getY())
{
fillMySquareV.add(new MyCoordinates(me.getX(), me.getY(), x1, y1, defaultFGColour));
}else{
fillMySquareV.add(new MyCoordinates(x1, y1, me.getX(), me.getY(), defaultFGColour));
}
}else{
if(x1 > me.getX() || y1 > me.getY())
{
fillMySquareV.add(new MyCoordinates(me.getX(), me.getY(), x1, y1, defaultFGColour));

}else{

fillMySquareV.add(new MyCoordinates(x1, y1, me.getX(), me.getY(), defaultFGColour));

}
}
}

repaint();
}

public void mouseMoved(MouseEvent me)
{

}
/*=============================================================================*/
public void paintComponent(Graphics g)
{
super.paintComponent(g);

redrawVectorBuffer(g);

g.setColor(defaultFGColour);

if (drawMode == mySquare)
{
if(myFillCheck)
{
if(x1 > x2 || y1 > y2)

g.fillRect(x2,y2,x1-x2,y1-y2);
else
g.fillRect(x1,y1,x2-x1,y2-y1);


}else{

if(x1 > x2 || y1 > y2)

g.drawRect (x2,y2,x1-x2,y1-y2);
else
g.drawRect (x1,y1,x2-x1,y2-y1);

}
}



public void setDrawMode(int d)
{
drawMode = d;
}

public int getDrawMode()
{
return drawMode;
}




private RenderedImage myCreateImage()
{
BufferedImage bufferedImage = new BufferedImage(600,390, BufferedImage.TYPE_INT_RGB);

Graphics g = bufferedImage.createGraphics();
redrawVectorBuffer(g);

g.dispose();
return bufferedImage;
}

private void redrawVectorBuffer(Graphics g)
{

for (int i=0;i<squareV.size();i++){
g.setColor(((MyCoordinates)squareV.elementAt(i)).colour());
g.drawRect(((MyCoordinates)squareV.elementAt(i)).getX1(),((MyCoordinates)squareV.elementAt(i)).getY1(),((MyCoordinates)squareV.elementAt(i)).getX2()-((MyCoordinates)squareV.elementAt(i)).getX1(),((MyCoordinates)squareV.elementAt(i)).getY2()-((MyCoordinates)squareV.elementAt(i)).getY1());
}

}

public class MyCoordinates implements Serializable
{
int x1, y1, x2, y2;
Color fColor;

public MyCoordinates(int xx1, int yy1, int xx2, int yy2, Color color)
{
x1 = xx1;
y1 = yy1;
x2 = xx2;
y2 = yy2;
fColor = color;

}

public Color colour()
{
return fColor;
}

public int getX1()
{
return x1;
}

public int getX2()
{
return x2;
}

public int getY1()
{
return y1;
}

public int getY2()
{
return y2;
}

}//End of class [MyCoordinates]

} //End of class
 
Martin Mathis
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My guess is that myPaintCanvas (in MyPaintButton) is null. Do you have the code for that?
[ January 09, 2006: Message edited by: Martin Mathis ]
 
O Joseph
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot buddy, that was the problem.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!