"AWT-EventQueue-0" java.lang.NullPointerException

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

Hey can someone explain why im getting this error?

I'm trying to load a hashmap object from a file. Saving works fine but loading seems to bug out.

Here's my code:

privatevoid jButton1ActionPerformed(java.awt.event.ActionEvent evt){

SaveAndLoad typesLoad =new SaveAndLoad(types);

typesLoad.loadFromFile();

}

publicclass SaveAndLoadimplements Serializable{

private HashMap types;

public SaveAndLoad(HashMap types)

{

this.types = types;

}

void loadFromFile(){

ObjectInputStream input =null;

try

{

input =new ObjectInputStream(new FileInputStream (

"./types.ser" ));

}

catch(IOException io)

{

System.out.println("Error opening the file");

}

try

{

//Retrieve object you saved.

SaveAndLoad types = (SaveAndLoad) input.readObject();

}

catch(IOException io)

{

System.out.println("Error reading file");

}

catch(ClassNotFoundException e)

{

System.out.println("Unable to create object");

}

try

{

if(input !=null)

input.close();

}

catch (IOException io)

{

System.out.println("Error closing file");

}

}

}

[2805 byte] By [KingScootya] at [2007-11-26 17:38:34]
# 1
Post the stacktrace.
kajbja at 2007-7-9 0:06:44 > top of Java-index,Java Essentials,New To Java...
# 2

at submission2.SaveAndLoad.loadFromFile(SaveAndLoad.java:80)

at submission2.MainForm.jButton1ActionPerformed(MainForm.java:280)

at submission2.MainForm.access$300(MainForm.java:18)

at submission2.MainForm$4.actionPerformed(MainForm.java:219)

at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)

at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)

at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)

at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)

at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)

at java.awt.Component.processMouseEvent(Component.java:6038)

at javax.swing.JComponent.processMouseEvent(JComponent.java:3260)

at java.awt.Component.processEvent(Component.java:5803)

at java.awt.Container.processEvent(Container.java:2058)

at java.awt.Component.dispatchEventImpl(Component.java:4410)

at java.awt.Container.dispatchEventImpl(Container.java:2116)

at java.awt.Component.dispatchEvent(Component.java:4240)

at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)

at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)

at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)

at java.awt.Container.dispatchEventImpl(Container.java:2102)

at java.awt.Window.dispatchEventImpl(Window.java:2429)

at java.awt.Component.dispatchEvent(Component.java:4240)

at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)

at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)

at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)

KingScootya at 2007-7-9 0:06:44 > top of Java-index,Java Essentials,New To Java...
# 3
The error is on line 80 in SaveAndLoad.javaWhat line is that?
kajbja at 2007-7-9 0:06:44 > top of Java-index,Java Essentials,New To Java...
# 4
SaveAndLoad recipes = (SaveAndLoad) input.readObject();Yea i wasnt quite sure if that line was 100% correct :S
KingScootya at 2007-7-9 0:06:44 > top of Java-index,Java Essentials,New To Java...
# 5

It means input is null, which also means that the line that instantiates input is throwing an exception that you are ignoring. Well, OK, you are printing a message, but you should print the stacktrace in the catch block. As a matter of fact you should always either print the stacktrace or log it to a file when an exception is thrown.

~Tim

SomeoneElsea at 2007-7-9 0:06:44 > top of Java-index,Java Essentials,New To Java...
# 6

> As a matter of fact you should always either print the stacktrace or log it to a file when an exception is thrown.

I think the use of the word "always" without qualification here is misleading. There are a multitude of cases where it makes more sense to simply handle the exception gracefully. I think you may be intending to advise the OP to never (or very, very rarely) swallow exceptions, which makes a whole lot of sense. But that's different from "always log a stack trace when an exception is thrown".

~

yawmarka at 2007-7-9 0:06:44 > top of Java-index,Java Essentials,New To Java...
# 7
Hmm, i'm pretty new to java, well at least using the netbeans IDE. Could you guys suggest some code that might fix the problem?
KingScootya at 2007-7-9 0:06:44 > top of Java-index,Java Essentials,New To Java...
# 8

Ok, always is an overstatement, and I started to make that point, but then changed my mind when I realized that would just confuse the issue. Most of the time is a more appropriate response there. However, even if it is a fairly minor problem, and one that you might actually expect, it never hurts to log the error. If you want, make it pretty instead of logging the entire trace as is, but never just swallow it whole.

~Tim

SomeoneElsea at 2007-7-9 0:06:44 > top of Java-index,Java Essentials,New To Java...
# 9
Can anyone help me fix the problem?If this line is causing the problems i must have set something up wrong. All the code is at the top of the page incase anyone finds the problem out before me :SSaveAndLoad types = (SaveAndLoad) input.readObject();
KingScootya at 2007-7-9 0:06:44 > top of Java-index,Java Essentials,New To Java...
# 10
That wasn't the full stacktrace, was it? Was your code outputting anything else, like "Error opening the file"?
CaptainMorgan08a at 2007-7-9 0:06:44 > top of Java-index,Java Essentials,New To Java...
# 11

> Can anyone help me fix the problem?

>

> If this line is causing the problems i must have set

> something up wrong. All the code is at the top of the

> page incase anyone finds the problem out before me

> :S

>

> SaveAndLoad types = (SaveAndLoad)

> input.readObject();

I'm sorry, is it hand-holding time again? OK, forget what I posted in reply #5, and follow along. Your code:

try

{

input = new ObjectInputStream(new FileInputStream (

"./types.ser" ));

}

catch(IOException io)

{

System.out.println("Error opening the file");

}

is failing to initialize input. You are not printing out the error, thus cannot figure out what th eproblem is. In the catch block, instead of just a s.o.p, you should have this

io.printStackTrace();

And you will see what the source problem is. If you can't figure that out, come back here and post the stack trace.

~Tim

Message was edited by:

SomeoneElse

SomeoneElsea at 2007-7-9 0:06:44 > top of Java-index,Java Essentials,New To Java...
# 12

Ah ok, i wasnt too sure what was a full stacktrace. That print stacktrace line helped me find the problem. Only thing is now ive solved that one, i have another problem:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.util.HashMap cannot be cast to submission2.SaveAndLoad

at submission2.SaveAndLoad.loadFromFile(SaveAndLoad.java:81)

at submission2.MainForm.jButton1ActionPerformed(MainForm.java:280)

at submission2.MainForm.access$300(MainForm.java:18)

at submission2.MainForm$4.actionPerformed(MainForm.java:219)

at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)

at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)

at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)

at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)

at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)

at java.awt.Component.processMouseEvent(Component.java:6038)

at javax.swing.JComponent.processMouseEvent(JComponent.java:3260)

at java.awt.Component.processEvent(Component.java:5803)

at java.awt.Container.processEvent(Container.java:2058)

at java.awt.Component.dispatchEventImpl(Component.java:4410)

at java.awt.Container.dispatchEventImpl(Container.java:2116)

at java.awt.Component.dispatchEvent(Component.java:4240)

at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)

at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)

at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)

at java.awt.Container.dispatchEventImpl(Container.java:2102)

at java.awt.Window.dispatchEventImpl(Window.java:2429)

at java.awt.Component.dispatchEvent(Component.java:4240)

at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)

at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)

at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)

KingScootya at 2007-7-9 0:06:44 > top of Java-index,Java Essentials,New To Java...
# 13
SaveAndLoad types = (SaveAndLoad) input.readObject();You're reading in a HashMap, then trying to cast it to a SaveAndLoad. They are two unrelated things.
CaptainMorgan08a at 2007-7-9 0:06:44 > top of Java-index,Java Essentials,New To Java...
# 14
Yeah i wasnt too sure how that function worked :/ ive never attempted to load an object before.
KingScootya at 2007-7-9 0:06:44 > top of Java-index,Java Essentials,New To Java...
# 15
>You're reading in a HashMap, then trying to cast it to a SaveAndLoad. They are two unrelated things.What should i do instead?
KingScootya at 2007-7-21 17:09:07 > top of Java-index,Java Essentials,New To Java...
# 16

> >You're reading in a HashMap, then trying to cast it

> to a SaveAndLoad. They are two unrelated things.

>

> What should i do instead?

Since this method is in the SaveAndLoad class, and the SaveAndLoad class has a private member variable of type HashMap, I assume what you should be doing is

try

{

//Retrieve object you saved.

this.types = input.readObject();

}

~Tim

SomeoneElsea at 2007-7-21 17:09:07 > top of Java-index,Java Essentials,New To Java...