graphics nullpointerexception help

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 430 Views - Last Post: 06 August 2012 - 09:33 AM Rate Topic: -----

#1 RookieCookie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 05-August 12

graphics nullpointerexception help

Posted 05 August 2012 - 12:16 PM

Alright,

I dont understand why or how but I am getting a nullpointerexception from this line of code:

public void drawFlag()
{
Graphics g = getGraphics();
g.drawImage(Ships.flag, mouseX, mouseY, null); // this is where I am getting pointed to by error. I have tryed changing the mouseX with 1 and mouseY with 1.
}



NOTE: the Ships.flag isn't the problem, I have a code almost identically to this that works:

public void drawShip(Graphics g)
{
g.draw(Ships.cruiser, x, y, null)
}



Is This A Good Question/Topic? 0
  • +

Replies To: graphics nullpointerexception help

#2 GregBrannon  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1986
  • View blog
  • Posts: 4,830
  • Joined: 10-September 10

Re: graphics nullpointerexception help

Posted 05 August 2012 - 12:23 PM

In code, 'almost identical' means nothing.

It's easy enough to verify whether Ships.flag or any of the other items is null: try printing it, use an if branch to avoid the line of code to isolate the null item, maybe a try/catch, etc.
Was This Post Helpful? 0
  • +
  • -

#3 RookieCookie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 05-August 12

Re: graphics nullpointerexception help

Posted 05 August 2012 - 12:29 PM

View PostGregBrannon, on 05 August 2012 - 12:23 PM, said:

In code, 'almost identical' means nothing.

It's easy enough to verify whether Ships.flag or any of the other items is null: try printing it, use an if branch to avoid the line of code to isolate the null item, maybe a try/catch, etc.


How do I print it?
Give me an example cause I dont wanna use whole tomorrow just trying to fix the problem :/
Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1986
  • View blog
  • Posts: 4,830
  • Joined: 10-September 10

Re: graphics nullpointerexception help

Posted 05 August 2012 - 12:40 PM

Before that line, add

System.out.println( "Ships.flag is : " + Ships.flag );

And look for the result in your console output. If it's null, you'll see "Ships.flag is null" If it's not null, you'll get an address or reference to it that has an '@' sign in the result.

Let's see . . . could that cause an error? Maybe, but it shouldn't. The toString() should accept it and deal with it. But if you do get an error, that'll also indicate that it's the source of the problem.
Was This Post Helpful? 1
  • +
  • -

#5 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2115
  • View blog
  • Posts: 8,810
  • Joined: 20-September 08

Re: graphics nullpointerexception help

Posted 05 August 2012 - 12:42 PM

Possibly 'g' is null. If you call getGraphics before a graphics context is available, it might well be. What you're doing is probably wrong anyway. Drawing should normally take place inside an overridden paint/paintComponent
Was This Post Helpful? 0
  • +
  • -

#6 RookieCookie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 05-August 12

Re: graphics nullpointerexception help

Posted 05 August 2012 - 12:58 PM

View Postg00se, on 05 August 2012 - 12:42 PM, said:

Possibly 'g' is null. If you call getGraphics before a graphics context is available, it might well be. What you're doing is probably wrong anyway. Drawing should normally take place inside an overridden paint/paintComponent


Yea, im pretty sure the g is the problem. I dont quite understand what you mean so could you give me an example? Would be much appreciated.

View PostGregBrannon, on 05 August 2012 - 12:40 PM, said:

Before that line, add

System.out.println( "Ships.flag is : " + Ships.flag );

And look for the result in your console output. If it's null, you'll see "Ships.flag is null" If it's not null, you'll get an address or reference to it that has an '@' sign in the result.

Let's see . . . could that cause an error? Maybe, but it shouldn't. The toString() should accept it and deal with it. But if you do get an error, that'll also indicate that it's the source of the problem.


Ok thanks, just needed to be sure the you meant system.out.printIn.
Was This Post Helpful? 0
  • +
  • -

#7 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2115
  • View blog
  • Posts: 8,810
  • Joined: 20-September 08

Re: graphics nullpointerexception help

Posted 05 August 2012 - 12:59 PM

Well let's not guess. First of all, please answer which reference(s) is/are null

btw, i now see this is a duplicate question. Please don't post duplicates

This post has been edited by g00se: 05 August 2012 - 01:04 PM
Reason for edit:: duplicate

Was This Post Helpful? 0
  • +
  • -

#8 RookieCookie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 05-August 12

Re: graphics nullpointerexception help

Posted 05 August 2012 - 01:12 PM

View Postg00se, on 05 August 2012 - 12:59 PM, said:

Well let's not guess. First of all, please answer which reference(s) is/are null



Im on my ipad atm so I cant do anything with code, wil have to wait for tomorrow.
And if I understood what you said about paint and paintComponents then I will have a problem. I can only have 1 paintComponent method but I need several so that the program may 'trigger' them when it needs to.

Example:
lets say that I have all the games ships inside the paintComponent, but I only need one of them how can I then 'filter' out the ship I want without using 'if'.

View Postg00se, on 05 August 2012 - 12:59 PM, said:

btw, i now see this is a duplicate question. Please don't post duplicates


I am sorry, this is a more "detailed" version of the question. How do I delete or edit topics/posts?
Was This Post Helpful? 0
  • +
  • -

#9 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2115
  • View blog
  • Posts: 8,810
  • Joined: 20-September 08

Re: graphics nullpointerexception help

Posted 05 August 2012 - 01:26 PM

Quote

How do I delete or edit topics/posts?

You can't. Don't worry about it this time.

Quote

And if I understood what you said about paint and paintComponents then I will have a problem. I can only have 1 paintComponent method but I need several so that the program may 'trigger' them when it needs to.

It seems like you have understood, since you ask a good question. Typically the flow is as follows:

a. the requirement is flagged to be painted (the 'flag' in your case ;))
b. repaint() is called
c. paintComponent then responds to the value of the flag(s)

In your case, you might want several flag graphics to show, in which case you could have a List<Flag> (yes, i know you don't have a Flag class - perhaps you could?) which is simply iterated in paintComponent
Was This Post Helpful? 1
  • +
  • -

#10 RookieCookie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 05-August 12

Re: graphics nullpointerexception help

Posted 05 August 2012 - 01:44 PM

View Postg00se, on 05 August 2012 - 01:26 PM, said:

a. the requirement is flagged to be painted (the 'flag' in your case ;))
b. repaint() is called
c. paintComponent then responds to the value of the flag(s)

In your case, you might want several flag graphics to show, in which case you could have a List<Flag> (yes, i know you don't have a Flag class - perhaps you could?) which is simply iterated in paintComponent



Link me a tutorial?
Was This Post Helpful? 0
  • +
  • -

#11 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2115
  • View blog
  • Posts: 8,810
  • Joined: 20-September 08

Re: graphics nullpointerexception help

Posted 06 August 2012 - 01:43 AM

http://docs.oracle.c...swing/painting/
Was This Post Helpful? 0
  • +
  • -

#12 RookieCookie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 05-August 12

Re: graphics nullpointerexception help

Posted 06 August 2012 - 02:40 AM

I have confirmed that the g is null.

And another problem with paintComponent is that I need to load an image with a mouse click. I cant do that with paintComponent(Graphics g).
Was This Post Helpful? 0
  • +
  • -

#13 RookieCookie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 05-August 12

Re: graphics nullpointerexception help

Posted 06 August 2012 - 02:56 AM

View Postg00se, on 06 August 2012 - 01:43 AM, said:




Thats with a rectangle, I need an image from file that can be drawn on the screen, not on startup of application but only when I click the mouse.
Was This Post Helpful? 0
  • +
  • -

#14 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2115
  • View blog
  • Posts: 8,810
  • Joined: 20-September 08

Re: graphics nullpointerexception help

Posted 06 August 2012 - 04:35 AM

That doesn't matter. The principle is the same.
Was This Post Helpful? 0
  • +
  • -

#15 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8029
  • View blog
  • Posts: 31,164
  • Joined: 06-March 08

Re: graphics nullpointerexception help

Posted 06 August 2012 - 04:35 AM

View PostRookieCookie, on 06 August 2012 - 05:40 AM, said:

I have confirmed that the g is null.

And another problem with paintComponent is that I need to load an image with a mouse click. I cant do that with paintComponent(Graphics g).

Sure you can

boolean paintImage = false;


public void mouseClicked(MouseEvent e) {
    paintImage = true;
    repaint();
}

public paintComponent(Graphics g) {
   super.paintComponent(g);
   if(paintImage) {
      ... draw your image here
   }
}


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2