2

Hey guys I get the errors

Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
        at P6.itemStateChanged(P6.java:215)
        at javax.swing.AbstractButton.fireItemStateChanged(AbstractButton.java:2
023)
        at javax.swing.AbstractButton$Handler.itemStateChanged(AbstractButton.ja
va:2325)
        at javax.swing.DefaultButtonModel.fireItemStateChanged(DefaultButtonMode
l.java:440)
        at javax.swing.JToggleButton$ToggleButtonModel.setSelected(JToggleButton
.java:255)
        at javax.swing.ButtonGroup.setSelected(ButtonGroup.java:147)
        at javax.swing.JToggleButton$ToggleButtonModel.setSelected(JToggleButton
.java:237)
        at javax.swing.JToggleButton$ToggleButtonModel.setPressed(JToggleButton.
java:272)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonL
istener.java:236)
        at java.awt.Component.processMouseEvent(Component.java:6267)
        at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
        at java.awt.Component.processEvent(Component.java:6032)
        at java.awt.Container.processEvent(Container.java:2041)
        at java.awt.Component.dispatchEventImpl(Component.java:4630)
        at java.awt.Container.dispatchEventImpl(Container.java:2099)
        at java.awt.Component.dispatchEvent(Component.java:4460)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577
)
        at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)

        at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
        at java.awt.Container.dispatchEventImpl(Container.java:2085)
        at java.awt.Component.dispatchEvent(Component.java:4460)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThre
ad.java:269)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.
java:184)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
ad.java:174)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)

        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)

        at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

In

import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.ButtonGroup;
import javax.swing.JApplet;
import javax.swing.JComboBox;
import javax.swing.JRadioButton;

public class P6 extends JApplet implements MouseListener, MouseMotionListener, ItemListener
{
   private final int RED = 1;
   private final int ORANGE = 2;
   private final int YELLOW = 3;
   private final int GREEN = 4;
   private final int BLUE = 5;
   private final int CYAN = 6;
   private final int PINK = 7;
   private final int MAGENTA =8;
   private final int BLACK = 9;
   private final int ARC = 1;
   private final int LINE = 2;
   private final int OVAL = 3;
   private final int RECT = 4;
   private final int POLY = 5;

   private String[] colorNames = { "Red", "Orange", "Yellow", "Green", "Blue",
   "Cyan", "Pink", "Magenta", "Black"};
   private JComboBox colorComboBox;
   private JRadioButton jrbArc;
   private JRadioButton jrbLine;
   private JRadioButton jrbOval;
   private JRadioButton jrbRect;
   private JRadioButton jrbPolygon;
   private int color = 0;
   private int shape = 0;
   private int x1;
   private int y1;
   private int x2;
   private int y2;

   public void init()
   {
      Container c = getContentPane();
      c.setLayout( new FlowLayout() );
      c.setBackground( Color.orange );
      JComboBox colorComboBox = new JComboBox(colorNames);
      colorComboBox.addItemListener(this);

      JRadioButton jrbArc = new JRadioButton("Arc");
      jrbArc.addItemListener(this);
      JRadioButton jrbLine = new JRadioButton("Line");
      jrbLine.addItemListener(this);
      JRadioButton jrbOval = new JRadioButton("Oval");
      jrbOval.addItemListener(this);
      JRadioButton jrbRect = new JRadioButton("Rectangle");
      jrbRect.addItemListener(this);
          JRadioButton jrbPolygon = new JRadioButton("Polygon");
      jrbPolygon.addItemListener(this);

      ButtonGroup btg = new ButtonGroup();
      btg.add(jrbArc);
      c.add(jrbArc);
      btg.add(jrbLine);
      c.add(jrbLine);
      btg.add(jrbOval);
      c.add(jrbOval);
      btg.add(jrbRect);
      c.add(jrbRect);
      btg.add(jrbPolygon);
      c.add(jrbPolygon);
      c.add(colorComboBox);

      addMouseMotionListener(this);
      addMouseListener(this);

   }

   public void paint(Graphics g)
   {
      super.paint(g);
      g.setColor(Color.white);
      g.fillRect(20, 50, 500, 500);
      g.setFont( new Font("Serif", Font.ITALIC+Font.BOLD, 18));
      g.setColor(Color.black);
      g.drawString("Start Here!", 10, 50);
      g.drawString("End Here!", 510, 560);

      switch (this.color)
      {
         case 1:
           g.setColor(Color.red);
           break;
         case 2:
           g.setColor(Color.orange);
           break;
         case 3:
           g.setColor(Color.yellow);
           break;
         case 4:
           g.setColor(Color.green);
           break;
         case 5:
           g.setColor(Color.blue);
           break;
         case 6:
           g.setColor(Color.cyan);
           break;           
         case 7:
           g.setColor(Color.pink);
           break;
         case 8:
           g.setColor(Color.magenta);
           break;
         case 9:
           g.setColor(Color.black);
           break;
      }

      switch (shape)
          {
        case 1:
          g.fillArc(this.x1, this.y1, this.x2 - this.x1, this.y2 - this.y1, 0, 90);
          g.fillArc(this.x1, this.y1, this.x2 - this.x1, this.y2 - this.y1, 180,90);
          break;
        case 2:
          g.drawLine(this.x1, this.y1, this.x2, this.y2);
          break;
        case 3:
          g.fillOval(this.x1, this.y1, this.x2 - this.x1, this.y2 - this.y1);
          break;
        case 4:
          g.fillRect(this.x1, this.y1, this.x2 - this.x1, this.y2 - this.y1);
          break;
        case 5:
          int[] arr1 = { this.x1, this.x2, this.x1, this.x2, this.x1};
          int[] arr2 = { this.y1, this.y1, this.y2 ,this.y2, this.y1};
          g.fillPolygon(arr1, arr2, 5);
          break;
      }
   }

   public void mousePressed( MouseEvent e)
   {
      this.x1 = e.getX();
      this.y1 = e.getY();
   }

   public void mouseDragged( MouseEvent e)
   {
      this.x2 = e.getX();
      this.y2 = e.getY();
   }

   public void mouseReleased( MouseEvent e)
   {
      this.x2 = e.getX();
      this.y2 = e.getY();
   }

  public void mouseClicked( MouseEvent e)
  {
  }

  public void mouseEntered( MouseEvent e)
  {
  }

  public void mouseExited( MouseEvent e)
  {
  }

  public void mouseMoved( MouseEvent e)
  {
    showStatus( " (" + e.getX() + ", " + e.getY() +")");
  }

  public void itemStateChanged(ItemEvent i)
  {
     if(this.jrbArc.isSelected())
       this.shape = ARC;
     else if( this.jrbLine.isSelected())
       this.shape = LINE;
     else if( this.jrbOval.isSelected())
       this.shape = OVAL;
     else if( this.jrbRect.isSelected())
       this.shape = RECT;
     else if( this.jrbPolygon.isSelected())
       this.shape = POLY;

     this.color = ( this.colorComboBox.getSelectedIndex() + 1 );
     repaint();
   }
}

I've been trying to figure this out, but no luck. Any help please?

Line 215 is:

if(this.jrbArc.isSelected())
1
  • Can you point out which line is 215 in P6? Commented Feb 23, 2011 at 3:06

3 Answers 3

4

instead of

JRadioButton jrbArc = new JRadioButton("Arc");

you need

jrbArc = new JRadioButton("Arc");

And similar stuff for other controls too.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you sooo much! It works now :) Can you explain to me the difference between them though? I'm not sure how exactly they differ.
@Kalvin as Jochen pointed out, JRadioButton jrbArc = new JRadioButton("Arc");' just creates a local variable and does not touch the class field variable private JRadioButton jrbArc;`
2

Always go to the first error/line mentioned. In this case, it's

P6.java:215

What do you find at this line? Do a null check, and trace it back.

4 Comments

line 215 is if(this.jrbArc.isSelected())
Well, you know this cannot be null. This means jrbArc must be null. Is it being set properly in the constructor, or otherwise?
I have jrbArc set as a JRadioButton in init() JRadioButton jrbArc = new JRadioButton("Arc");
Is it possible you're firing your ItemStateChangedEvent before you're calling init()?
2

You have a naming conflict: jrbArc is a field and used as a local variable. You are not actually setting the field, thus the NullPointerException

1 Comment

Can you explain what you mean? Im kind of confused.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.