aspose file tools*
The moose likes Beginning Java and the fly likes null Pointer Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "null Pointer Exception" Watch "null Pointer Exception" New topic
Author

null Pointer Exception

Justin Kuyken
Greenhorn

Joined: Aug 26, 2004
Posts: 4
hi
im getting a nullPointerException on line 114 and 159 when i try to run the program after compiling the code succesfully,
Any help with why would be greatly appreciated.
(Errors Marked in Bold)

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

public class AssignSem2 extends JFrame
{
private JButton b,con;
private JRadioButton b1,b2,b3;
private ButtonGroup radioGroup;
private JList list;
private String listStuff[]={"E 6 (20%)","E 12 (10%)","E 24 (05%)"};
private JTextField in, out;
private GridLayout grid;
private Graphics rb,rb1,rb2,rb3,rb4,rb5; //resistor base, resistor band 1-4
/************************ Creating The GUI *****************************/
public AssignSem2()
{
super ("Unit Converter");
/********************** File Menu **************************/
JMenu fileMenu = new JMenu( "File");
fileMenu.setMnemonic('F');

JMenuItem exitItem = new JMenuItem("Exit");
exitItem.setMnemonic('x');
fileMenu.add(exitItem);
exitItem.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}
}
);
JMenuBar bar = new JMenuBar();
setJMenuBar(bar);
bar.add(fileMenu);

JMenu helpMenu = new JMenu("Help");
helpMenu.setMnemonic('H');

JMenuItem aboutItem = new JMenuItem( "About...");
aboutItem.setMnemonic( 'A' );
helpMenu.add(aboutItem);
aboutItem.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
new About();
}
});
JMenuItem instructionsItem = new JMenuItem("Instructions");
instructionsItem.setMnemonic( 'I' );
helpMenu.add(instructionsItem);
instructionsItem.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
new Instructions();
}
});
bar.add(helpMenu);

/*************************************************************/
grid = new GridLayout(3,3,5,5); // Creating Grid Layout

Container c=getContentPane(); // Creates Main Window
c.setLayout(grid);

in=new JTextField ("Insert Value Here",15);
c.add(in); // Creates Input Field

con=new JButton("->Convert->"); // Creates Convert Button
c.add(con);

out=new JTextField ("Output",15); // Output Field
out.setEditable(false);
c.add(out);

b1=new JRadioButton("Ohms");
c.add(b1);
b2=new JRadioButton("Farads"); // Radoi Buttons
c.add(b2);
b3=new JRadioButton("Henrys");
c.add(b3);

radioGroup = new ButtonGroup(); // Groups Radio Buttons
radioGroup.add(b1);
radioGroup.add(b2);
radioGroup.add(b3);

b=new JButton("Quit"); // Quit Button
b.setToolTipText("Push The Button to exit");
b.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e ){
System.exit(0);
}
}
);
c.add(b);

list=new JList (listStuff);
list.setVisibleRowCount(3); // Tolerence List
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
c.add(new JScrollPane(list));

rb.setColor(new Color(255,231,186)); <-Error Here
rb.fillRect(250,115,100,20);

rb1.setColor(new Color(255,231,186));
rb1.fillRect(270,115,10,20);
rb2.setColor(new Color(255,231,186));
rb2.fillRect(290,115,10,20);
rb3.setColor(new Color(255,231,186));
rb3.fillRect(310,115,10,20);
rb4.setColor(new Color(255,231,186));
rb4.fillRect(330,115,10,20);
rb5.setColor(new Color(255,231,186));
rb5.fillRect(350,115,10,20);

addWindowListener
(
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);

setSize(360,180);
setLocation(100,100); // Sets size and location
setResizable(false); // of main window
show();
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
this.getContentPane().setBackground(new java.awt.Color(205,205,205));
this.setFont(new java.awt.Font("Sans Serif", 0, 12));
}
/************************* Main Method **********************************/
public static void main (String args[])
{
new AssignSem2(); <-Error Here
}
}
Justin Kuyken
Greenhorn

Joined: Aug 26, 2004
Posts: 4
Sorry i forgot to add the exact error i get:

java.lang.NullPointerException
at AssignSem2.<init>(AssignSem2.java:114)
at AssignSem2.main(AssignSem2.java:159)
Exception in thread "main"
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392

rb (and other rbX) is initialized to null and you're trying to invoke a method of a null object. As a result, NullPointerException is thrown. You must reference rb to a Graphic object before invoking rb.setColor(...).

Joyce
[ August 26, 2004: Message edited by: Joyce Lee ]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
When posting code, please be sure to surround the code with the [code] and [/code] UBB Tags. This will help to preserve the formatting of the code, thus making it easier to read and understand.

Note that you have the ability to edit your own posts - just click on the icon above your post that looks a bit like a piece of paper.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: null Pointer Exception