• Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting NullPointerException while working with Synchronization  RSS feed

 
Abhimanyu Jain
Ranch Hand
Posts: 151
1
Eclipse IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Need help with this code as it is giving out NullPointerException while running it. Please help
 
Henry Wong
author
Sheriff
Posts: 23280
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

First, let's fix the code tags...

Abhimanyu Jain wrote:

Need help with this code as it is giving out NullPointerException while running it. Please help


Hint: when is the "s" variable, that is used by the run() method, set?

Henry
 
Abhimanyu Jain
Ranch Hand
Posts: 151
1
Eclipse IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, it was helpful but still I am not getting the result as desired. Still getting some exception. Please check again for me one more time. Thanks in advance

I was using 's' without initializing it. I corrected that. But still it is not working.
 
Henry Wong
author
Sheriff
Posts: 23280
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhimanyu Jain wrote:Hi, it was helpful but still I am not getting the result as desired. Still getting some exception. Please check again for me one more time. Thanks in advance

I was using 's' without initializing it. I corrected that. But still it is not working.


You really should consider giving us some information. Read this... TellTheDetails

Henry
 
Abhimanyu Jain
Ranch Hand
Posts: 151
1
Eclipse IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry sir, here is the code.



class SynchronizedTest implements Runnable
{

Thread t;
SynchronizedTest(String name)
{
t=new Thread(this);
t.setName(name);
System.out.println("Thread : "+t);
t.start();
}

public void run()
{
SynchronizedTest s=new SynchronizedTest("Joke");
s.printNum();
}

synchronized void printNum()
{
try
{
for(int i=1;i<=5;i++)
{
System.out.println(i);
Thread.sleep(1000);
}
}
catch(InterruptedException e)
{
System.out.println(e);
return;
}
}
}

public class SynchronizedDemo
{
public static void main(String[] args)
{
SynchronizedTest st=new SynchronizedTest("First");
SynchronizedTest st1=new SynchronizedTest("Second");
}
}

 
Abhimanyu Jain
Ranch Hand
Posts: 151
1
Eclipse IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you please share the link for code tags as well? I will be grateful.
 
Henry Wong
author
Sheriff
Posts: 23280
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhimanyu Jain wrote:Sorry sir, here is the code.


Okay, let's try this again...

Abhimanyu Jain wrote:Hi, it was helpful but still I am not getting the result as desired. Still getting some exception. Please check again for me one more time. Thanks in advance

I was using 's' without initializing it. I corrected that. But still it is not working.


Still getting some exception? What exception? How about a stack trace?

It is not working? What do you think is supposed to happen? Why do you think that? What is actually happening?

Henry
 
Abhimanyu Jain
Ranch Hand
Posts: 151
1
Eclipse IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It should print

Thread : Thread [First,5,main]
1
2
3
4
5

Thread : Thread [Second,5,main]
1
2
3
4
5

But when I run the program, it runs in an infinite loop.
 
Henry Wong
author
Sheriff
Posts: 23280
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhimanyu Jain wrote:
But when I run the program, it runs in an infinite loop.


Basically, you have a recursive call. Follow your code, and you will see that your SynchronizedTest class, is creating another SynchronizedTest class, which is creating another SynchronizedTest class, etc.

Henry
 
Abhimanyu Jain
Ranch Hand
Posts: 151
1
Eclipse IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Please help me correct this code.
 
Henry Wong
author
Sheriff
Posts: 23280
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhimanyu Jain wrote:
Please help me correct this code.


What have you tried so far? And what regarding my previous response are you having an issue with?

Remember that we are NotACodeMill -- we can help out, but not provide full solutions.

Henry
 
Abhimanyu Jain
Ranch Hand
Posts: 151
1
Eclipse IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I already corrected the code. Thanks!!
 
Henry Wong
author
Sheriff
Posts: 23280
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhimanyu Jain wrote:I already corrected the code. Thanks!!


Good to hear. And thanks for coming back and telling us ... have a cow.

Henry
 
Abhimanyu Jain
Ranch Hand
Posts: 151
1
Eclipse IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!