Exception in thread
I'm having a problem with a small program. First there is the class GUI, which includes all UI stuff. When I click a specific button in this UI, a command is sent to the class Actions, to launch this code:
(SuperKommando is made from the class "Supern")
public String RegistrerEier( String EierNavn, String EierAdresse, String EierType, String EierNummer ){
String Returstring = EierNavn + EierAdresse + EierNummer;
boolean Registrer =true;
if ( EierNavn =="" ){
Registrer =false;
}
if ( EierAdresse =="" ){
Registrer =false;
}
if ( EierNummer =="" ){
Registrer =false;
}
if ( Registrer ==true ){
if ( EierType =="Firma" ){
//FirmaEier = new Firmaer();
//Start en kode i Supern
}else{
//PersonEier = new Personer();
//Start en kode i Supern
SuperKommando.NyPerson(EierNavn, EierAdresse);//Program doesn't like this line
Returstring = SuperKommando.Test();//Program doesn't like this line
Returstring +=" er lagt til";
}
}
return Returstring;
}
When the code above launches, these two codesets are run in the class "Supern":
publicvoid NyPerson(String EierNavn, String EierAdresse)
{
Personer nyperson =new Personer( EierNavn, EierAdresse );//sett inn navnet til personen
Person.settInn( nyperson );
}
public String Test(){
return"reaksjon";
}
However before the codesets above are run, these errors occur and the program stops:
Exception in thread"AWT-EventQueue-0" java.lang.NullPointerException
at Actions.RegistrerEier(Actions.java:30)
at GUI.RegBileier(GUI.java:327)
at GUI.actionPerformed(GUI.java:198)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
I dont understand what's wrong...
[4923 byte] By [
Athenea] at [2007-11-26 18:55:50]

SuperKommando is obviously null, though it's hard to tell why without seeing more code.
What more code should I paste? The code in the GUI-class, launching the codes that dont work?
In that case, here:
public void RegBileier() {
String EierType = "";
Utskrift.setVisible(true);
if ( EierNummeret.equals("Foretaksnummer:") ) {
EierType = "Firma";
} else
EierType = "Person";
Utskrift.setText(Kommando.RegistrerEier( EierNavn.getText(), EierAdresse.getText(), EierType, EierNummer.getText() ));
}
Kommando = private Actions Kommando;
I don't know if you realize this, but you may run into problems using == with
strings, because that doesn't test if 2 strings contain the same sequence of
characters; instead it tests if two references point to the same string object.
It's safer to write:
if ("".equals(s))
True, I tend to forget that I can't use == with String in java, I'm not used to using .equals all the time in other languages
> True, I tend to forget that I can't use == with> String in java, I'm not used to using .equals all the> time in other languagesJava gets that comment/complaint all the time. I'm surprised that Javacompilers don't emit warnings about that.
Hm. I copied over the 4 most needed classes to a new project, the error changed slightly, now Im stuck at a bit easier(?) problem.
Same code as before in the class Actions:
public String RegistrerEier( String EierNavn, String EierAdresse, String EierType, String EierNummer ) {
String Returstring = EierNavn + EierAdresse + EierNummer;
boolean Registrer = true;
if ( EierNavn.equals("") ) {
Registrer = false;
}
if ( EierAdresse.equals("") ) {
Registrer = false;
}
if ( EierNummer.equals("") ) {
Registrer = false;
}
if ( Registrer == true ) {
if ( EierType.equals("Firma") ) {
Returstring = "eier er firma og ble ikke lagt til";
} else {
Returstring = SuperKommando.Test(); //programmet liker ikke denne linja
}
}
return Returstring;
}
Returstring = SuperKommando.Test(); this goes to this code:
public String Test() {
return "reaksjon";
}
Trying to launch SuperKommando.Test(), I get these errors again:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Actions2.RegistrerEier(Actions2.java:33) (the first code pasted)
at GUI2.RegBileier(GUI2.java:345) (code launching the one above)
at GUI2.actionPerformed(GUI2.java:200) (code launching the one above)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
I don't know which line exactly has the exception, but it looks like one of the Strings passed to RegistrerEier is null.
The error doesn't happen untill Returstring = SuperKommando.Test(); is run, but that code doesn't send any Strings or other variables, so why would it break at that point even if any of the Strings would be null? It should break before it get's that far, shouldn't it?
Allright look at this very simplified program:
The UI-class:
import java.awt.Container;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import java.awt.event.*;
public class GUI extends JFrame implements ActionListener {
private JTextArea Utskrift;
private JButton Action;
private Abstrakt Temp2;
private UAbstrakt Temp3;
public GUI(){
super( "Registrering av biler og eiere" );
Utskrift = new JTextArea( 10, 34 );
Action = new JButton( "Push" );
Action.setActionCommand("Action");
Utskrift.setEditable( false );
Action.addActionListener(this);
Container c = getContentPane();
c.setLayout( new FlowLayout() );
c.add( Action );
c.add( new JScrollPane( Utskrift ) );
setSize( 390, 300 );
setVisible( true );
}
public void actionPerformed(ActionEvent e) {
Utskrift.setText(Temp2.Test());
Temp2.Test2();
Utskrift.setText(Temp3.Test());
Temp3.Test2();
}
}
Abstrakt-class:
public abstract class Abstrakt {
public String Test() {
return "reaksjon";
}
public void Test2() {
}
}
UAbstrakt-class:
public class UAbstrakt {
public String Test() {
return "reaksjon";
}
public void Test2() {
}
}
I'm getting the exact same errors here, nomatter what code I try to call in Abstrakt or UAbstrakt... Why?
You never actually initialize private Abstrakt Temp2; private UAbstrakt Temp3;