Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

end of a thread's execution throws java.lang.NullPointerException

Posted on 2006-11-18
4
Medium Priority
?
221 Views
Last Modified: 2010-03-31
Hi,

I made an application, call it a.java, that creates 2 thread objects, call them t1 and t2.   t1 and t2 each use a parser object to parse an xml file.  a.java creates the two parser objects, call them p1 and p2, and passes them to t1, and t2, respectively. There is no "communication" between the parser objects and a.java object.
When one of the threads finishes, the other thread dies. The error I get is
Exception in thread "Thread-2" java.lang.NullPointerException
        at iroom.xmlp.process(xmlp.java:153)
        at iroom.xmlp.parse(xmlp.java:87)
        at iroom.ParserRunner.run(ParserRunner.java:29)
        at java.lang.Thread.run(Thread.java:595)
 where iroom is the package, xmlp is the parser class from which p1 and p2 are instantiated, and ParserRunner is the class from which t1 and t2 are instatiated, and it implements Runnable interface.
When I only create one thread the program finishes without any errors.
I am new to threads in Java, and would appreciate any help in solving this problem.

Mirna
0
Comment
Question by:mirnalim
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 

Author Comment

by:mirnalim
ID: 17973501
I forgot to add that a.java creates two instances of a third class, Tag, call them tag1 and tag2, and gives tag1 to t1, and tag2 to t2. Since each thread receives its own copy of the xmlp object and Tag object I do not synchronize any of the calls.
0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 17973525
can you post process method? (line no 153)
0
 

Author Comment

by:mirnalim
ID: 17973527
I just found out that when one of the theads finishes execution the control is given back to the main function in a.java instance, which then exist and this throws the null pointer exception. So how do I make the main function wait until all of the threads are finished executing?
0
 
LVL 23

Accepted Solution

by:
Ajay-Singh earned 2000 total points
ID: 17973538
call join()

t1.join();
t2.join();
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question