• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

null Pointer Exception

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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"
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Screaming fools! It's nothing more than a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic