0

I've got an applet with radio buttons to select the color of the line, but when I try to run it, I get this error:

java.lang.NullPointerException

This is what I have thus far. Any suggestions on making color selection work?

import java.awt.*;
import java.applet.*;
import javax.swing.*;
import java.awt.event.*;

public class ProjectPaint extends Applet {

  private JRadioButton redButton = new JRadioButton("Red");
  private JRadioButton blueButton = new JRadioButton("Blue");
  private JRadioButton greenButton = new JRadioButton("Green");
  private JRadioButton blackButton = new JRadioButton("Black");
    Graphics g;

  public void init() {
    // Create a border layout design
     setLayout(new BorderLayout());
    // Make a new object of type DrawPanel
     DrawPanel dp = new DrawPanel();
    // Add a draw panel in the center
     add("Center", dp);
    // Add another draw panel for color selection on top
     add("North",new DrawControls(dp));
  }

  int x1;
  int y1;
  class DrawPanel extends Panel
  {
     public boolean mouseDown(Event e, int x, int y)
     {
     // User has started a mouse drag.
     // Remember where:
        x1 = x;
        y1 = y;

        return true;
     }

     public boolean mouseDrag(Event e, int x, int y)
     {
     // User is continuing a mouse drag.
     // Draw line from last point to this
     // point:
            g = getGraphics();
        g.drawLine( x1,y1, x,y );

     // Remember new "last point":
        x1 = x;
        y1 = y;

        return true;
     }
  }
  class DrawControls extends Panel
  {
     public DrawControls(DrawPanel target)
     {
        setLayout(new BorderLayout());
        JPanel panel = new JPanel();
        ButtonGroup radioButtonGroup = new ButtonGroup();
        radioButtonGroup.add(redButton);
        radioButtonGroup.add(blueButton);
        radioButtonGroup.add(greenButton);
        radioButtonGroup.add(blackButton);
        panel.add(redButton);
        panel.add(blueButton);
        panel.add(greenButton);
        panel.add(blackButton);
        add(panel, BorderLayout.NORTH);
        redButton.addActionListener(new RadioButtonListener());
        blueButton.addActionListener(new RadioButtonListener());
        greenButton.addActionListener(new RadioButtonListener());
        blackButton.addActionListener(new RadioButtonListener());
            blackButton.doClick();
     }
  }
  private class RadioButtonListener implements ActionListener
  {
     public void actionPerformed(ActionEvent e)
     {
            int color = -1;
        if(e.getSource() == redButton)
        {
                g.setColor(Color.red);
        }
        if(e.getSource() == blueButton)
        {
                g.setColor(Color.blue);
        }
        if(e.getSource() == greenButton)
        {
                g.setColor(Color.green);
        }
        if(e.getSource() == blackButton)
        {
                g.setColor(Color.black);
        }
     }
  }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Tony White
  • 197
  • 5
  • 18

1 Answers1

3

The Graphics reference has not been assigned hence the NPE.

Don't attempt any custom painting from an ActionListener or MouseListener. Instead use a Color class member variable to set the color. For Swing applications, all custom painting should be done in the paintComponent method. In this case DrawPanel will need to changed so as to extend JPanel so that it can override the method. Add the @Override annotation and make sure to invoke super.paintComponent(g).

JApplet has support for the JFC/Swing component architecture so use that also.

Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • so I have a variable set up below `Graphics g;` as the very top called `int color = 0;. I changed my actionperformed to update the int with a number, and I made a paintComponent method that sets the color according to the `int` value of `color`. I get no error, but the color doesn't change when I select a radio button. – Tony White May 28 '13 at 18:22
  • `java.awt.Panel` does not extend `JComponent` - add an `@Override ` annotation and see for yourself. Don't use a `Graphics` object as a class member variable - use the one in `paintComponent`. Instead use a `Color` reference rather than `int`. Also see my updates – Reimeus May 28 '13 at 18:26
  • I'm so terrible at this, dunno why I went into computer science ha. I'll look over the answer and try to figure it out. Changing it to JPanel made my DrawPanel not come up at all, but I figured you probably expected that. As for using a color reference, I'm not sure how I would do that – Tony White May 28 '13 at 18:32
  • Your `drawLine` statements go in the `paintComponent` method – Reimeus May 28 '13 at 18:35
  • So with the code above, I need to 1) Write a `paintComponent` method 2) Move `Graphics g;` down to my `paintComponent` method. 3) Move all painting methods from `ActionListener` and `MouseListener` to the `paintComponent` 4) Change `extends Panel` to `extends JPanel` and put in `@Override` inside the `DrawPanel` method. 5) Invoke `super.paintComponent(g)`... inside the `paintComponent` method?.. Is that everything I need to fix? – Tony White May 28 '13 at 19:40
  • `paintComponent` already has a Graphics object so just use that. BTW I don't see any `addMouseListener` method call for `DrawPanel`... – Reimeus May 28 '13 at 20:17