Results 1 to 2 of 2
  1. #1
    sapata is offline Member
    Join Date
    May 2008
    Posts
    2
    Rep Power
    0

    Default 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!

    Java 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 .. ;)

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    C:\jexp>java ScreenTest
    Exception in thread "main" java.lang.NoSuchMethodError: main
    Added a main method and try again.
    Java Code:
    C:\jexp>java ScreenTest
    Exception in thread "main" java.lang.NullPointerException
            at ScreenTest.returnX(screentest.java:22)
            at ScreenTest.main(screentest.java:49)
    Java 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 [i]new[/i] operator, eg,
            // [i]dm = new DisplayMode(width, height, bitDepth, refreshRate);[/i]
            // 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 [i]returnX[/i] 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);
        }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •