Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Exception in thread "main" java.lang.NullPointerException at Draw.<init>(Draw.java:78), at Draw.main(Draw.java:152)

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Exception in thread "main" java.lang.NullPointerException at Draw.<init>(Draw.java:78), at Draw.main(Draw.java:152)

    import java.awt.*;
    import java.awt.event.*;

    public class Draw extends Frame implements ActionListener, ItemListener {

    // Initial Frame size
    static final int WIDTH = 800; // frame width
    static final int HEIGHT = 600; // frame height

    // Color choices
    static final String COLOR_NAMES[] = {"None", "Red", "Blue", "Green"};
    static final Color COLORS[] = {null, Color.red, Color.blue, Color.green};

    // Button control
    Button circle;
    Button roundRec;
    Button threeDRec;
    Button line;
    Button square;
    Button oval;
    Button clear;
    Button reset;

    // Color choice box
    Choice colorChoice;

    // the canvas
    DrawCanvas canvas;

    /**
    * Constructor
    */
    public Draw() {
    super("Java Draw");
    setLayout(new BorderLayout());

    // create panel for controls
    Panel topPanel = new Panel(new GridLayout(3, 1));
    add(topPanel, BorderLayout.NORTH);

    // create button control
    Panel buttonPanel = new Panel(new FlowLayout(FlowLayout.LEFT));
    topPanel.add(buttonPanel);

    circle = new Button("Circle");
    buttonPanel.add(circle);
    roundRec = new Button("Rounded Rectangle");
    buttonPanel.add(roundRec);
    threeDRec = new Button("3D Rectangle");
    buttonPanel.add(threeDRec);

    Panel buttonPanel1 = new Panel(new FlowLayout(FlowLayout.LEFT));
    topPanel.add(buttonPanel1);

    line = new Button("Line");
    buttonPanel1.add(line);
    square = new Button("Square");
    buttonPanel1.add(square);
    oval = new Button("Oval");
    buttonPanel1.add(oval);

    // add button listener
    circle.addActionListener(this);
    roundRec.addActionListener(this);
    threeDRec.addActionListener(this);
    line.addActionListener(this);
    square.addActionListener(this);
    oval.addActionListener(this);
    clear.addActionListener(this);
    reset.addActionListener(this);

    // create panel for color choices
    Panel colorPanel = new Panel(new FlowLayout(FlowLayout.LEFT));
    topPanel.add(colorPanel);
    Label label = new Label("Filled Color:");
    colorPanel.add(label);
    colorChoice = new Choice();
    for(int i=0; i<COLOR_NAMES.length; i++) {
    colorChoice.add(COLOR_NAMES[i]);
    }
    colorPanel.add(colorChoice);
    colorChoice.addItemListener(this);

    // create the canvas
    canvas = new DrawCanvas();
    add(canvas, BorderLayout.CENTER);

    Panel buttonPanel2 = new Panel(new FlowLayout(FlowLayout.LEFT));
    topPanel.add(buttonPanel2);

    clear = new Button("Clear");
    buttonPanel2.add(clear);
    reset = new Button("Reset");
    buttonPanel2.add(reset);
    } // end of constructor


    /**
    * Implementing ActionListener
    */
    public void actionPerformed(ActionEvent event) {
    if(event.getSource() == circle) { // circle button
    canvas.setShape(DrawCanvas.CIRCLE);
    }
    else if(event.getSource() == roundRec) { // rounded rectangle button
    canvas.setShape(DrawCanvas.ROUNDED_RECTANGLE);
    }
    else if(event.getSource() == threeDRec) { // 3D rectangle button
    canvas.setShape(DrawCanvas.RECTANGLE_3D);
    }
    else if(event.getSource() == line) { // 3D rectangle button
    canvas.setShape(DrawCanvas.LINE);
    }
    else if(event.getSource() == square) { // 3D rectangle button
    canvas.setShape(DrawCanvas.SQUARE);
    }
    else if(event.getSource() == oval) { // 3D rectangle button
    canvas.setShape(DrawCanvas.OVAL);
    }
    else if(event.getSource() == clear) { // 3D rectangle button
    Graphics g = null;
    canvas.paint(g);
    }
    else if(event.getSource() == reset) { // 3D rectangle button
    canvas.setShape(DrawCanvas.CIRCLE);
    Color color = COLORS[0];
    }
    }

    /**
    * Implementing ItemListener
    */
    public void itemStateChanged(ItemEvent event) {
    Color color = COLORS[colorChoice.getSelectedIndex()];
    canvas.setFilledColor(color);
    }

    /**
    * the main method
    */
    public static void main(String[] argv) {
    // Create a frame
    Draw frame = new Draw();
    frame.setSize(WIDTH, HEIGHT);
    frame.setLocation(150, 100);

    // add window closing listener
    frame.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent event) {
    System.exit(0);
    }
    });

    // Show the frame
    frame.setVisible(true);
    }
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,855
    My Mood
    Mellow
    Thanks
    178
    Thanked 631 Times in 618 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException at Draw.<init>(Draw.java:78), at Draw.main(Draw.java:152)

    Too much code, improperly posted, too little conversation. Read the Announcement topic at the top of the sub-forum to learn how to use the forum.

Similar Threads

  1. I get the error Exception in thread "main" java.lang.NullPointerException?
    By jeremy28 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: October 3rd, 2013, 11:13 PM
  2. Replies: 1
    Last Post: April 7th, 2013, 03:40 PM
  3. Replies: 5
    Last Post: December 9th, 2012, 01:25 PM
  4. Exception in thread "main" java.lang.NullPointerException problem.......
    By Adam802 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 20th, 2012, 02:23 AM
  5. Replies: 3
    Last Post: December 7th, 2011, 01:03 AM