Jump to content

NullPointerException... Help please.

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
gszauer

gszauer

    Programmer

  • Members
  • PipPipPipPip
  • 113 posts
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:
[HIGHLIGHT="Java5"]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();
}
}[/HIGHLIGHT]

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

~Aristotle said:

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

    Programmer

  • Members
  • PipPipPipPip
  • 115 posts
I'm not familiar with this, but

public void setLayout(LayoutManager mgr)

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

#3
gszauer

gszauer

    Programmer

  • Members
  • PipPipPipPip
  • 113 posts
That's not it, i use null for about 80% of my gui apps...

~Aristotle said:

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

    Programmer

  • Members
  • PipPipPipPip
  • 113 posts
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();
}


[HIGHLIGHT="Java5"]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();
}
}[/HIGHLIGHT]

~Aristotle said:

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