Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Global Variable Help

setpreferredsize global variable

  • Please log in to reply
3 replies to this topic

#1 Cruel Hand

Cruel Hand

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 195 posts
  • Programming Language:Java, Objective-C, Visual Basic .NET
  • Learning:C, Java, C++, Objective-C, PHP, (Visual) Basic, Python, JavaScript, Perl, Ruby, PL/SQL, Pascal, Assembly, Haskell

Posted 31 May 2012 - 10:17 AM

I'm learning about global (static) variables and I tried using a static graphics variable and I don't know what I'm doing wrong. I think it's something with using the name "g", but I'm not sure.

public class Peach extends JPanel{
public static Graphics g;
public Peach(){
  setPreferredSize(new Dimension(200, 200));
}

public void paintComponent(Graphics g){
  g.setColor(Color.RED);
  g.fillRect(2, 2, 196, 196);
  displayFont(Color.red, "Monospaced", 2, "martino", 15);
}
public static void displayFont(Color c, String fontName, int style, String message, int y){
  g.setColor(c);
  g.setFont(new Font(fontName, style, 20));
  g.drawString(message, 15, y);
}
}

here is the error:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Peach.displayFont(Peach.java:16)
at Peach.paintComponent(Peach.java:13)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JLayeredPane.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paintToOffscreen(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
at javax.swing.RepaintManager.paint(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
at java.awt.Container.paint(Unknown Source)
at java.awt.Window.paint(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.access$700(Unknown Source)
at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
thanks!
  • 0

#2 Sinipull

Sinipull

    CC Addict

  • Validating
  • PipPipPipPipPip
  • 384 posts

Posted 31 May 2012 - 10:34 AM

You never initialize the g field. its value is currently null, and that's why you get the null pointer exception. You can't call methods for a null reference.

Also your graphics field is static, but you can't really use that in that context, because your instances of Peach have all their own separate Graphics. So the static g really doesn't make much sense. Unless you only produce one Peach object and assign its Graphics to the g. But it's still bad technique, because you can access the object's Graphics by getGraphics() method, which is inherited from the JPanel.
  • 0
.

#3 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts
  • Location:Argentina
  • Programming Language:C, C++, C#, PHP, JavaScript, Transact-SQL, Bash, Others
  • Learning:Java, Others

Posted 31 May 2012 - 01:16 PM

There sinipull just gave you a detailed answer, it is not complicated to solve that :D
  • 0

#4 Cruel Hand

Cruel Hand

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 195 posts
  • Programming Language:Java, Objective-C, Visual Basic .NET
  • Learning:C, Java, C++, Objective-C, PHP, (Visual) Basic, Python, JavaScript, Perl, Ruby, PL/SQL, Pascal, Assembly, Haskell

Posted 31 May 2012 - 01:35 PM

thank you for the detailed response, sinipull. What you are saying certainly makes sense, it's just graphics has always been sort of confusing for me and my book is doing something similar to what I did. However, my book is using custom classes instead of JFrame and JPanel, so I guess because I was trying to use the actual classes and not use the books', that's where the confusion lies.

thanks
  • 0





Also tagged with one or more of these keywords: setpreferredsize, global variable

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download