• Post Reply Bookmark Topic Watch Topic
  • New Topic

null pointer exception

 
Shae Oommen
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i need to do a graphics program where there are 2 circles and i have to draw a line connecting these.....
the circles are done.....but when i try to draw the line there is Null pointer exception

try
{
gp.drawLine(cord[0],cord[1],cord[2],cord[3]);
repaint();
}
catch(Exception e){
System.out.println(e);
}

initialization is done as
int[] cord=new int[4];
Graphics g_dp;
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Shae Oommen:
i need to do a graphics program where there are 2 circles and i have to draw a line connecting these.....
the circles are done.....but when i try to draw the line there is Null pointer exception

try
{
gp.drawLine(cord[0],cord[1],cord[2],cord[3]);
repaint();
}
catch(Exception e){
System.out.println(e);
}

initialization is done as
int[] cord=new int[4];
Graphics g_dp;



The only place in the code you've provided where I see the possibility of a NullPointerException is that gp has not been initialized.
 
Shae Oommen
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even when i give

Graphics g_dp=null;

the same error occurs
 
Paul Clapham
Sheriff
Posts: 21863
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Shae Oommen:
Even when i give

Graphics g_dp=null;

the same error occurs
But that has nothing to do with the variable named "gp".
 
Shae Oommen
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry its not gp its g_dp...

try
{
g_dp.drawLine(cord[0],cord[1],cord[2],cord[3]);
repaint();
}
catch(Exception e){
System.out.println(e);
}
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course you get a NPE if you call g_dp.drawLine after setting g_dp=null. Are you setting g_dp to a valid value somewhere?
 
Shae Oommen
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
g_dp has nto been given any other value.Its been used in that line only

g_dp.drawLine(cord[0],cord[1],cord[2],cord[3]);

so what needs to be done?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
g_dp needs to point to an actual java.awt.Graphics object -- the same one you used to draw the circles!
 
Shae Oommen
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!