Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

NullPointerException... Help please.

calculator null

  • Please log in to reply
3 replies to this topic

#1 gszauer

gszauer

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 112 posts

Posted 20 January 2008 - 12:22 PM

Hey all, me again.
Im making a Java ROI Calculator, and my java file compiles, but the class file will not execute. it give me the following error:

> java RoiCalculator
NullPointerException:
at RoiCalculator.<init>(RoiCalculator.java:20)
at RoiCalculator.main(RoiCalculator.java:46)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)


My code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

//implements ActionListener
public class RoiCalculator extends JFrame {
int width, height, x, y;
double[] values = new double[12];
JTextField textFields[];
JLabel labels[];
JButton reset;

public RoiCalculator() {
Container pane = getContentPane();
pane.setLayout(null);

textFields = new JTextField[12];
labels= new JLabel[12];

textFields[0].setLocation(419, 40);
textFields[1].setLocation(419, 76);
textFields[2].setLocation(419, 112);
textFields[3].setLocation(419, 152);
textFields[4].setLocation(419, 192);
textFields[5].setLocation(191, 297);
textFields[6].setLocation(191, 345);
textFields[7].setLocation(191, 397);
textFields[8].setLocation(191, 437);
textFields[9].setLocation(419, 297);
textFields[10].setLocation(419, 345);
textFields[11].setLocation(419, 397);
width = 69; height = 22;
for (int i = 0; i < labels.length; i++) {
textFields[i].setSize(69, 22);
// Add Action Listener
pane.add(textFields[i]);
}

setTitle("Java Calculator");
setSize(519, 456);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}

public static void main(String[] args){
RoiCalculator ROICalc = new RoiCalculator();
}
}


Can anyone please help?
Thank you for your time
~Gabor
  • 0

It is the mark of an educated mind to entertain a tought without accepting it

If my post was helpful, please help me build some rep Posted Image

#2 icepack

icepack

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 106 posts

Posted 20 January 2008 - 01:53 PM

I'm not familiar with this, but

public void setLayout(LayoutManager mgr)

and you are passing it null, which it might not like?
  • 1

#3 gszauer

gszauer

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 112 posts

Posted 20 January 2008 - 02:01 PM

That's not it, i use null for about 80% of my gui apps...
  • 0

It is the mark of an educated mind to entertain a tought without accepting it

If my post was helpful, please help me build some rep Posted Image

#4 gszauer

gszauer

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 112 posts

Posted 20 January 2008 - 06:09 PM

I forgot to initialize the individual text fields.
This bit-o code solved it:
for (int i = 0; i < textFields.length; i++){
textFields[i] = new JTextField();
}


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

//implements ActionListener
public class RoiCalculator extends JFrame {
int width, height, x, y;
double[] values = new double[12];
JTextField textFields[];
JLabel labels[];
JButton reset;

public RoiCalculator() {
Container pane = getContentPane();
pane.setLayout(null);

textFields = new JTextField[12];
labels= new JLabel[12];

for (int i = 0; i < textFields.length; i++){
textFields[i] = new JTextField();
}

textFields[0].setLocation(419, 40);
textFields[1].setLocation(419, 76);
textFields[2].setLocation(419, 112);
textFields[3].setLocation(419, 152);
textFields[4].setLocation(419, 192);
textFields[5].setLocation(191, 297);
textFields[6].setLocation(191, 345);
textFields[7].setLocation(191, 397);
textFields[8].setLocation(191, 437);
textFields[9].setLocation(419, 297);
textFields[10].setLocation(419, 345);
textFields[11].setLocation(419, 397);
width = 69; height = 22;
for (int i = 0; i < textFields.length; i++) {
textFields[i].setSize(width, height);
// Add Action Listener
pane.add(textFields[i]);
}

setTitle("Java Calculator");
setSize(519, 456);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}

public static void main(String[] args){
RoiCalculator ROICalc = new RoiCalculator();
}
}

  • 1

It is the mark of an educated mind to entertain a tought without accepting it

If my post was helpful, please help me build some rep Posted Image





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download