Results 1 to 5 of 5
  1. #1
    andre1011 is offline Member
    Join Date
    Feb 2009
    Location
    Sacramento CA
    Posts
    17
    Rep Power
    0

    Default Null Pointer Exception

    I am starting a drawing application where the user will be able to draw simple shapes with a mouse. The program complies with the "JGRASP" complier but when I run the program it crashes and displays

    " Exception in thread "main" java.lang.NullPointerException
    at DesktopFrame.<init>(DesktopFrame.java:165)
    at mainDrawingApp.main(mainDrawingApp.java:27) "
    below are all of my classes

    {code}
    /*
    file: DesktopFrame.java
    author: Andre Long
    date: Febuary 3, 2009
    purpose: To control all frames
    */
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import javax.swing.JFrame;
    import javax.swing.JDesktopPane;
    import javax.swing.JMenuBar;
    import javax.swing.JMenu;
    import javax.swing.JMenuItem;
    import javax.swing.JInternalFrame;
    import javax.swing.JPanel;
    import java.awt.Container;
    import java.awt.GridLayout;
    import javax.swing.JButton;
    import javax.swing.ImageIcon;
    import javax.swing.Icon;
    import java.awt.Dimension;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
    import java.awt.event.MouseEvent;
    import java.awt.Color;

    public class DesktopFrame extends JFrame
    {
    private JDesktopPane theDesktop; //the main frame
    private Container container;
    private JInternalFrame toolbox;
    private andreFrame blankCanvas;
    private JButton circleButton; //various JButtons
    private JButton sphereButton;
    private JButton squareButton;
    private JButton rectangleButton;
    private JButton triangleButton;
    private JButton cylinderButton;
    private JButton straightLineButton;
    private JButton ploygonButton;
    private enum shapeChoice{CIRCLE,SHPERE,SQUARE,RECTANGLE,TRIANGL E,CYLINDER,STRAIGHTLINE,PLOYGON};//enumeration

    DesktopFrame()
    {
    super("Main Container for all Frames");
    theDesktop = new JDesktopPane();
    add(theDesktop);

    JMenuBar bar = new JMenuBar(); //create a menu bar
    JMenu menuHeaderShapes = new JMenu("Shapes"); //create a menu Header
    JMenu menuHeaderCanvas = new JMenu("Drawing Canvas"); //create a menu Header
    JMenu menuHeaderActions = new JMenu("Actions"); //create a menu Header

    JMenuItem firstMenuItemShapes = new JMenuItem("Show Shapes");//create submenu item
    menuHeaderShapes.add(firstMenuItemShapes); //add submenu item

    JMenuItem secondMenuItemShapes = new JMenuItem("Hide Shapes");//create submenu item
    menuHeaderShapes.add(secondMenuItemShapes); //add submenu item

    JMenuItem firstMenuItemCanvas = new JMenuItem("New Canvas");//create submenu item
    menuHeaderCanvas.add(firstMenuItemCanvas); //add submenu item

    JMenuItem secondMenuItemCanvas = new JMenuItem("Colors");//create submenu item
    menuHeaderCanvas.add(secondMenuItemCanvas); //add submenu item

    JMenuItem thirdMenuItemCanvas = new JMenuItem("texture");//create submenu item
    menuHeaderCanvas.add(thirdMenuItemCanvas); //add submenu item


    JMenuItem firstMenuItemActions = new JMenuItem("Clear Canvas");//create submenu item
    menuHeaderActions.add(firstMenuItemActions); //add submenu item

    JMenuItem secondMenuItemActions = new JMenuItem("Undo");//create submenu item
    menuHeaderActions.add(secondMenuItemActions); //add submenu item

    bar.add(menuHeaderShapes); //add a menu to the menu bar
    bar.add(menuHeaderCanvas); //add a menu to the menu bar
    bar.add(menuHeaderActions); //add a menu to the menu bar


    setJMenuBar(bar); //set menu bar for this application


    firstMenuItemShapes.addActionListener(
    new ActionListener()
    {
    public void actionPerformed(ActionEvent event)
    { /* constructor for JInternalFrame takes a title, and boolean values for
    resize, close, maximize, minimize
    */
    toolbox = new JInternalFrame("toolbox",true,true,true,true);
    /* toolbox.setBounds(200,200,150,350); sets the "x","y",width and height we do not need because of
    setSize() and setLocation below
    */
    container = toolbox.getContentPane();
    Icon circle = new ImageIcon(getClass().getResource("images/circle.jpg"));
    Icon sphere = new ImageIcon(getClass().getResource("images/sphere.jpg"));
    Icon square = new ImageIcon(getClass().getResource("images/square.jpg"));
    Icon rectangle = new ImageIcon(getClass().getResource("images/rectangle2.jpg"));
    Icon triangle = new ImageIcon(getClass().getResource("images/triangle.jpg"));
    Icon cylinder = new ImageIcon(getClass().getResource("images/cylinder1.jpg"));
    Icon straightLine = new ImageIcon(getClass().getResource("images/straightLine.jpg"));
    Icon ploygonal = new ImageIcon(getClass().getResource("images/ploygonal.jpg"));
    container.setLayout(new GridLayout(4,2));
    container.add(circleButton = new JButton(circle));
    container.add(sphereButton = new JButton(sphere));
    container.add(squareButton = new JButton(square));
    container.add(rectangleButton = new JButton(rectangle));
    container.add(triangleButton = new JButton(triangle));
    container.add(cylinderButton = new JButton(cylinder));
    container.add(straightLineButton = new JButton(straightLine));
    container.add(ploygonButton = new JButton(ploygonal));
    theDesktop.add(toolbox);
    toolbox.setVisible(true);
    toolbox.setSize(150,200);
    toolbox.setLocation(0,0);

    }//end of actionPerformed
    }//end of ActionListener
    );//end Action Listener for firstMenuItemShapes


    secondMenuItemShapes.addActionListener(
    new ActionListener()
    {
    public void actionPerformed(ActionEvent event)
    {
    toolbox.setVisible(false);
    }//end of actionPerformed

    }//end of ActionListener


    );//end Action Listener for secondMenuItemShapes


    firstMenuItemCanvas.addActionListener(
    new ActionListener()
    {
    public void actionPerformed(ActionEvent event)
    { /* constructor for JInternalFrame takes a title, and boolean values for
    resize, close, maximize, minimize
    */

    blankCanvas = new andreFrame("Drawing Canvas",true,true,true,true);

    /* toolbox.setBounds(200,200,150,350); sets the "x","y",width and height we do not need because of
    setSize() and setLocation below
    */
    container = blankCanvas.getContentPane();
    theDesktop.add(blankCanvas);
    blankCanvas.setVisible(true);
    blankCanvas.setSize(300,300);
    blankCanvas.setLocation(200,10);


    }//end of actionPerformed
    }//end of ActionListener
    );//end Action Listener for firstMenuItemCanvas

    circleButtonHandler fred = new circleButtonHandler();
    circleButton.addActionListener(fred); /* connects the J buttons to an action to take */


    MouseHandler mouseHandler = new MouseHandler();
    blankCanvas.addMouseListener(mouseHandler);
    blankCanvas.addMouseMotionListener(mouseHandler);

    //blankCanvas.setTextStausBar();

    }//end DesktopFrame constructor

    private class circleButtonHandler implements ActionListener
    {
    public void actionPerformed(ActionEvent event)
    {
    shapeChoice shapeChoiceStatus;
    shapeChoiceStatus = shapeChoice.CIRCLE; //stores the fact the user clicked the circle button

    }

    }//end of ActionListener for circleButton

    public class MouseHandler implements MouseListener,MouseMotionListener
    {
    public void mouseClicked(MouseEvent event)
    {
    blankCanvas.setTextStatusBar( String.format("Clicked at [%d,%d]",event.getX(),event.getY() ) );
    }//end of mouseClicked function

    public void mousePressed(MouseEvent event)
    {
    blankCanvas.setTextStatusBar( String.format("Pressed at [%d,%d]",event.getX(),event.getY() ) );
    }//end of mousePressed function

    public void mouseReleased(MouseEvent event)
    {
    blankCanvas.setTextStatusBar( String.format("Released at [%d,%d]",event.getX(),event.getY() ) );
    }//end of mouseRelease function

    public void mouseEntered(MouseEvent event)
    {
    blankCanvas.setTextStatusBar( String.format("Mouse entered at [%d,%d]",event.getX(),event.getY() ) );
    blankCanvas.setBackground(Color.GREEN);
    }//end of mouseEntered function

    public void mouseExited(MouseEvent event)
    {
    blankCanvas.setTextStatusBar("Mouse outside blankCanvas ");
    blankCanvas.setBackground(Color.WHITE);
    }//end of mouseExited function

    public void mouseDragged(MouseEvent event)
    {
    blankCanvas.setTextStatusBar( String.format("Mouse Draged at [%d,%d]",event.getX(),event.getY() ) );
    }//end of mouseDragged function

    public void mouseMoved(MouseEvent event)
    {
    blankCanvas.setTextStatusBar( String.format("Moved at [%d,%d]",event.getX(),event.getY() ) );
    }//end of mouseMoved function


    }//end of mouse interface


    }//end class DesktopFrame

    /*
    file: andreFrame.java
    author: Andre Long
    date: Febuary 3, 2009
    purpose: To constuct the drawing canvas to draw shapes
    */
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import javax.swing.JFrame;
    import javax.swing.JInternalFrame;
    import javax.swing.JPanel;
    import java.awt.Container;
    import java.util.Vector;
    import java.awt.Color;
    import javax.swing.JLabel;

    public class andreFrame extends JInternalFrame
    {
    private Vector<shape> vectorShapes; //must declare vector with a generic datatype of shape
    private int shapeCount; //holds the number of shapes in the vector called vectorShapes
    private int shapeType; //holds a number which determines the type of shape to draw
    private shape currentShape; //represents the current shape the user is drawing
    private Color currentColor; //represents the current drawing color
    private boolean filledShape; //determines weather to draw a filled shape
    private JLabel statusBar; //the status bar will display the coordinates of the current mouse position

    andreFrame(String title, boolean resizable, boolean closeable, boolean maximizable, boolean iconifiable)
    {
    super(title,resizable,closeable,maximizable,iconif iable);
    vectorShapes = new Vector<shape>();
    add(statusBar);


    }//end of constructor andreFRame

    public void addShapeVector(shape someShape)
    {
    vectorShapes.add(someShape);
    }//end of mehtod addShapeVector

    protected void paintComponent(Graphics g)//overides the method paintComponent from JComponent
    {

    }//end of paintComponent

    public void setShapeType(int numShapeType)
    {

    }//end of setShapeType

    public void setCurrentColor(Color x)
    {

    }//end of setCurrentColor

    public void setFilledShape(boolean trueFalse)
    {

    }//end of setFilledShape

    public void clearLastShape()//removes the last shape drawn
    {
    shapeCount--;
    if(shapeCount < 0)
    {
    shapeCount = 0;
    }
    }//end of clearLastShape

    public void clearDrawing()//removes all shapes in the current drawing
    {
    shapeCount = 0;
    }//end of clearDrawing

    public void setTextStatusBar(String someString)
    {
    statusBar.setText(someString);
    }

    }//end of class andreFrame

    /*
    File: mainDrawingApp.java
    Author: Andre Long
    Date: January 27, 2009
    Purpose: To serve as the main program which calls other classes for this drawing application
    */

    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import javax.swing.JFrame;
    import javax.swing.JDesktopPane;
    import javax.swing.JMenuBar;
    import javax.swing.JMenu;
    import javax.swing.JMenuItem;
    import javax.swing.JInternalFrame;
    import javax.swing.JPanel;


    public class mainDrawingApp
    {

    public static void main(String args[])
    {
    DesktopFrame theDesktop = new DesktopFrame();
    theDesktop.setDefaultCloseOperation(JFrame.EXIT_ON _CLOSE);
    theDesktop.setSize(800,480);
    theDesktop.setVisible(true);

    }// end main()

    } //end class mainDrawingApp
    {code}

    I left out the shapes class because I am not using it yet
    Can anyone tell me why I am getting a null pointer exception?
    I believe I highlighted the problem lines with underlines
    Last edited by andre1011; 02-07-2009 at 01:17 AM. Reason: to insert code tags

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,313
    Blog Entries
    1
    Rep Power
    31

    Default

    I would recommend that you edit your post so that you use code tags. This will allow your code will be well-formatted and readable. To do this, either highlight your code block and press the "code" button at the top above the message block or place the tag &#91;code&#93; at the top of your block of code and the tag &#91;/code&#93; at the bottom, like so:

    Java Code:
    {cod&#101;}
       // your code block goes here
       // notice that the top and bottom tags are identical here
    {cod&#101;}
    Also, indicate with "loud" comments just where the errors are occurring -- which line.

  3. #3
    andre1011 is offline Member
    Join Date
    Feb 2009
    Location
    Sacramento CA
    Posts
    17
    Rep Power
    0

    Default

    I inserted code tags and underlined the suspected problem lines

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,313
    Blog Entries
    1
    Rep Power
    31

    Default

    oops, my bad. I posted the tags for the Sun forum. For here use these:
    Java Code:
    [cod&#101;]
       // your code block goes here
       // notice that the top and bottom tags are different here
    [/cod&#101;]
    Very sorry!

  5. #5
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    12

    Default

    circleButton initialized in actionPerformed of firstMenuItemShapes.

    before that action perform, circleButton is null
    null object cant call its method
    Last edited by mtyoung; 02-07-2009 at 04:33 AM.

Similar Threads

  1. Null pointer Exception
    By peiceonly in forum New To Java
    Replies: 8
    Last Post: 09-05-2010, 07:48 PM
  2. Null Pointer Exception
    By ScKaSx in forum New To Java
    Replies: 1
    Last Post: 01-24-2009, 12:27 PM
  3. Null Pointer Exception
    By demiser55 in forum New To Java
    Replies: 1
    Last Post: 09-22-2008, 07:33 PM
  4. null pointer exception
    By cityguy503@yahoo.com in forum New To Java
    Replies: 4
    Last Post: 08-22-2008, 08:22 PM
  5. getting a null pointer exception
    By Rjava in forum XML
    Replies: 4
    Last Post: 07-16-2008, 06:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •