Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I keep getting NullPointerExeptions...

Status
Not open for further replies.

Savale

Technical User
Jun 14, 2001
17
0
0
US
Ok.. what I'm doing is taking text from a JTextField, and putting it into a String array.

Starray[0] = text1.getText();
Starray[1] = text2.getText();

Every thing works fine until this line is executed, and I get a NullPointerException. Is there something I'm doing wrong here or what?
 
Hi Savale,

Are you sure that you have initialized JTextField?
If you try to refer to object that is not initialized, you get that NullPointerException.
 
...and make sure that Starray isn't null either.
 
JTextField was initialized properly. I have that directly after declaring the class.
here is all the code if it helps anyone...

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

public class Dmath extends javax.swing.JFrame implements ActionListener {
String num3[];

JButton mult = new JButton("*");
JButton div = new JButton("/");
JLabel num2 = new JLabel("Second Number: ");
JTextField text2 = new JTextField(3);
JLabel num1 = new JLabel("First Number: ");
JTextField text1 = new JTextField(3);
JTextField text3 = new JTextField(15);
JButton add = new JButton("+");
JButton subt = new JButton("-");

public Dmath(){
initComponents();
pack();
}

private void initComponents(){
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
}
);
}

private void exitForm(java.awt.event.WindowEvent evt) {
System.exit(0);
}

public static void main(String[] args){

Dmath mainWindow = new Dmath();
mainWindow.addParams(mainWindow);
mainWindow.show(false);
mainWindow.resize(150, 200);
mainWindow.show(true);
}

public void addParams(Dmath mnWin){

FlowLayout fl = new FlowLayout();
mnWin.getContentPane().setLayout(fl);

add.addActionListener(this);
subt.addActionListener(this);
mult.addActionListener(this);
div.addActionListener(this);

mnWin.getContentPane().add(num1);
mnWin.getContentPane().add(text1);
mnWin.getContentPane().add(num2);
mnWin.getContentPane().add(text2);
mnWin.getContentPane().add(add);
mnWin.getContentPane().add(subt);
mnWin.getContentPane().add(mult);
mnWin.getContentPane().add(div);
mnWin.getContentPane().add(text3);

}

public void actionPerformed(java.awt.event.ActionEvent p1) {
Object src = p1.getSource();
Doug dg;

if (src.equals(add)){
dg = new Doug();

num3[0] = text1.getText();
num3[1] = text1.getText();

try {
dg.GetSumInts(num3);
text3.setText(String.valueOf(dg.SumOf()));
}
catch (Exception e) {
System.out.println("Error: BAD MAN!!");
System.exit(0);
}

}
else if (src.equals(subt)){
dg = new subt();
}


}

}

Its just a simple calculator program, and I know its not very pretty, but any suggestions would be helpful.

thanks
 
I don't see anywhere that you have initialized the array num3. You declare it as a class variable but never allocate any memory for it. You need to do:

int [] num3 = new int[2];

or however big you need it to be.

Charles
 
Erm...its a String array..not int array :) If you need additional help, you can email to me at zaoliang@hotmail.com I don't guaranty that I will be able to solve your problems but I will try my best :)
 
don't you need something like

starray [0] = new String(text1.getText());

or am i talking out of my arse as usual?
 
meadandale and LeonTang are right - the array has to be initialized first...

If you do text1.getText() you'll receive a String anyway. If you now create a
Code:
new String(text1.getText())
this does no harm - it just creates a new String object. If you create your own classes that return a (normally in your class private) String you'll probably want to create a new String out of them - as otherwise you pass the reference, and if it's modified afterwards you modify the private element that you initially wanted to protect...
But it's not really necessary here ;-)
allow thyself to be the spark that lights the fire
haslo@haslo.ch - www.haslo.ch​
 
Thank you much.. I actually got it late yesterday, I'm still a little used to programming in C++ still, and forget every once in a while. But thanks for all the help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top