Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-17-2008, 08:56 PM
Member
 
Join Date: May 2008
Posts: 2
sapata is on a distinguished road
getHeight() problem
Hey guys,

wondering if you could help me out with the following:
Im trying to create a small class which detects the user's screen resolution and stores it in attributes.

The following code generates a NullPointerException and I can't see why.

Thanks in advance!

Code:
import java.awt.*; public class ScreenTest { private int x; private int y; private String error; private DisplayMode dm; public ScreenTest() { x = 0; y = 0; error = null; } public int getRefresh(){ return dm.getRefreshRate(); } public int returnX(){ try { x = dm.getWidth(); } catch (HeadlessException hexc) { error = "Failed to initialize value for X"; } return x; } public int returnY(){ try { y = dm.getHeight(); } catch (HeadlessException hexc) { error = "Failed to initialize value for Y"; } return y; } public void resetResolution() { x = 0; y = 0; } public String returnError() { return error; } }
Please don't flame me if i am totally stupid ..
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 05-18-2008, 06:16 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,266
hardwired is on a distinguished road
Code:
C:\jexp>java ScreenTest Exception in thread "main" java.lang.NoSuchMethodError: main
Added a main method and try again.
Code:
C:\jexp>java ScreenTest Exception in thread "main" java.lang.NullPointerException at ScreenTest.returnX(screentest.java:22) at ScreenTest.main(screentest.java:49)
Code:
import java.awt.*; public class ScreenTest { private int x; private int y; private String error; // The jvm initializes this to null. If it is not // instantiated then all uses of the "dm" variable // will cause a NullPointerException because it // does not point to an object, viz, it is null. private DisplayMode dm; public ScreenTest() { x = 0; y = 0; error = null; } public int getRefresh(){ // "dm" is null so you cannot use it here without causing // a NullPointerException. If you instantiate it with // the new operator, eg, // dm = new DisplayMode(width, height, bitDepth, refreshRate); // in which you supply proper values for each of the arguments, // then the reference/variable "dm" will point to the new // object and no longet be null. return dm.getRefreshRate(); } public int returnX(){ try { x = dm.getWidth(); } catch (HeadlessException hexc) { error = "Failed to initialize value for X"; } return x; } public int returnY(){ try { y = dm.getHeight(); } catch (HeadlessException hexc) { error = "Failed to initialize value for Y"; } return y; } public void resetResolution() { x = 0; y = 0; } public String returnError() { return error; } public static void main(String[] args) { ScreenTest test = new ScreenTest(); // Threw a NullPointerException in the returnX method. // test.returnX(); // Explore the local environment: GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment() .getDefaultScreenDevice(); DisplayMode[] modes = gd.getDisplayModes(); for(int i = 0; i < modes.length; i++) { DisplayMode mode = modes[i]; query(modes[i]); } } private static void query(DisplayMode mode) { int bitDepth = mode.getBitDepth(); int height = mode.getHeight(); int refreshRate = mode.getRefreshRate(); int width = mode.getWidth(); System.out.printf("bitDepth = %2d refreshRate = %2d " + "width = %4d height = %d%n", bitDepth, refreshRate, width, height); } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +3. The time now is 03:03 PM.


VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org