Solved

Null Graphics Object

Posted on 2004-09-14
17
213 Views
Last Modified: 2010-03-31
Hey Experts!

What does it mean when the Graphics object that is related to a certain JPanel is null at a certain point of your program when you want to use it?

for example:

TestJPanel.getGraphics.drawline(...); //gives a null pointer exception becasue of the null graphics object
0
Comment
Question by:mte01
  • 8
  • 5
  • 4
17 Comments
 
LVL 92

Expert Comment

by:objects
ID: 12060842
probably because there is not one available (eg. panel isn't visible)
you really shouldn't use getGraphics() and only use the Graphics object passed to you in the paint methods.
Why do you need it outside the paint() methods?

0
 
LVL 3

Author Comment

by:mte01
ID: 12060859
for(int j=0,curheight=0;j<descno;j++)
        {
          LCCol curcol = (LCCol)curLCTab.getRowAt(i).elementAt(inc+j);
          lblspecs[f][j] = new JLabel(curcol.getColLabelRep()+": "+curcol.getColValue());
          jPanel5rep2[f].add(lblspecs[f][j]);
        }
        jPanel10rep2.add(jPanel5rep2[f],new XYConstraints(15,panelheight,-1,-1));
        panelheight += jPanel5rep2[f].getPreferredSize().getHeight() + 10;
        this.getGraphics().drawLine(0,panelheight+(int)jPanel3.getPreferredSize().getHeight(),150,panelheight+(int)jPanel3.getPreferredSize().getHeight());
0
 
LVL 3

Author Comment

by:mte01
ID: 12060860
Sorry ignore the previous code......
0
Independent Software Vendors: 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!

 
LVL 3

Author Comment

by:mte01
ID: 12060867
                 for(int j=0,curheight=0;j<descno;j++)
        {
          LCCol curcol = (LCCol)curLCTab.getRowAt(i).elementAt(inc+j);
          lblspecs[f][j] = new JLabel(curcol.getColLabelRep()+": "+curcol.getColValue());
          jPanel5rep2[f].add(lblspecs[f][j]);
        }
        jPanel10rep2.add(jPanel5rep2[f],new XYConstraints(15,panelheight,-1,-1));
        jPanel5rep2[f].getGraphics().drawLine(0,panelheight+(int)jPanel3.getPreferredSize().getHeight(),150,panelheight+(int)jPanel3.getPreferredSize().getHeight());

This is part of the function that I am using..........I think the panel is visible when I am using the Graphics object........Moreover, the program is very large, and I want to draw lines at the end of certain JPanels in certain functions in my program (that's why it would be very difficult to use paint....
0
 
LVL 3

Expert Comment

by:CI-Ia0s
ID: 12060868
It means that TestJPanel has no graphics object or is not currently displayable. Make sure that you have declared and initialized TestJPanel and that TestJPanel is not null.
0
 
LVL 3

Expert Comment

by:CI-Ia0s
ID: 12060875
I'd suggest having a for loop where you declare the entire array of panels, if that's possible...
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 12060876
> and I want to draw lines at the end of certain JPanels in certain functions in my program

problem with that is that your painting will be lost if your window is obscured , or the window is resized, or any other event occurs that requires the window to be repainted.

I'd suggest instead painting to an offscreen image, and painting that image to the screen in your paint method.
0
 
LVL 3

Expert Comment

by:CI-Ia0s
ID: 12060878
Declare and construct, I should say...
0
 
LVL 3

Expert Comment

by:CI-Ia0s
ID: 12060882
Or you could do what objects says, cause he's always right (and it *does* make more sense). ;)
0
 
LVL 92

Expert Comment

by:objects
ID: 12060892
> cause he's always right

dunno about that :)
0
 
LVL 3

Author Comment

by:mte01
ID: 12060897
>> I'd suggest having a for loop where you declare the entire array of panels, if that's possible...

I am doing that but I forgot to include that part in the code:

for(int i=0,f=0;i<rowno;i++)
      {
      if(gvec.contains(new Integer(i)))
      {
        jPanel5rep2[f] = new JPanel(new GridLayout(descno,1));
        for(int j=0,curheight=0;j<descno;j++)
        {
          LCCol curcol = (LCCol)curLCTab.getRowAt(i).elementAt(inc+j);
          lblspecs[f][j] = new JLabel(curcol.getColLabelRep()+": "+curcol.getColValue());
          jPanel5rep2[f].add(lblspecs[f][j]);
        }
        jPanel10rep2.add(jPanel5rep2[f],new XYConstraints(15,panelheight,-1,-1));
        panelheight += jPanel5rep2[f].getPreferredSize().getHeight() + 10;
        jPanel5rep2[f].getGraphics().drawLine(0,panelheight+(int)jPanel3.getPreferredSize().getHeight(),150,panelheight+(int)jPanel3.getPreferredSize().getHeight());
        f++;
      }
               ................
              .................

0
 
LVL 3

Author Comment

by:mte01
ID: 12060923
>> your painting will be lost if your window is obscured , or the window is resized, or any other event occurs that requires the window to be repainted.

I drew a line using the getGraphics method (in another function) of the applet that I am using (this.getGraphics()...), and I tested the program using the appletviewer and in a Web Browser, and I changed the size of the window, minimized it, navigated away from it and returned back......etc, but there were no problems.....
0
 
LVL 92

Expert Comment

by:objects
ID: 12060937
It may work sometimes, but there is no guarantee. As soon as the panel needs repainting for whatever reason its paint methods will get called and they paint whatever is there.
0
 
LVL 3

Author Comment

by:mte01
ID: 12060994
You can see the full source at the following adress: http://students.aub.edu.lb/~mte01/lcfilemain.zip

The getGraphics() method is used inside "Applet5.java"....If you feel that the paint method can be used properly for repainting the JPanels (and maybe the JApplet)......please do post your method
0
 
LVL 92

Expert Comment

by:objects
ID: 12061326

its a bit hard to work out exactly what your code is doing.
looks like you could simplify things by creating some seperate classes for some of your panels

a side observation is you don't need to sychronise any methods dealing with your gui as they should only ever get called from the one thread.
0
 
LVL 3

Author Comment

by:mte01
ID: 12061385
>>its a bit hard to work out exactly what your code is doing.

I know, and I'll be asking many questions about it later :), an you'll be getting many points from them :)......I'll be dropping a few notes of explanation for you about what the whole application is trying to do sometimes later

>> you could simplify things by creating some seperate classes for some of your panels

Yes I thought that would solve the problem.....it's just that I have many Panels to draw, and in each panel I want to draw a certain line at a certain place (some at the beginning, some at the end, some small, other medium or big....etc)....so I would have to write a class for almost each type of panel I am using which would be really cumbersome

>>you don't need to sychronise any methods dealing with your gui as they should only ever get called from the one thread.

Yes I read some documentation about that, and write now I am only letting the main thread call these functions (not any created thread)....despite that, sometimes I get an error of "current thread not main thread" upon reaching a line of code that creates a certain GUI object......synchronized solved this problem :) !!
0
 
LVL 3

Author Comment

by:mte01
ID: 12074907
I'll keep this question posted for like a day to see if anyone still can have a solution for my problem...
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

685 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