Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 14 of 14

Thread: Please Help Me With This Error!

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Please Help Me With This Error!

    Hi, I have attempted to make a puzzle game where you get a broken up puzzle and you need to solve it with the various pieces.

    I keep getting this error. I have no clue as to why I am getting this error. I searched on google to see what it means and I figured it out that it is different for every situation
    here is the error:
    java.lang.NullPointerException
    at Puzzle.init(Puzzle.java:57)
    at sun.applet.AppletPanel.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

    I also believe it cannot get the images width correctly for some odd reason

    import java.applet.Applet;
    import java.awt.*;
    import java.awt.image.*;
    import java.util.*;

    public class Puzzle extends Applet
    {
    private final boolean debug = false;

    private boolean secondClick = false;

    private final int pref = 72; // prefered tile size

    private int firstCol = -1;
    private int firstRow = -1;

    private int pieceWidth;
    private int pieceHeight;

    private int puzzleWidth;
    private int puzzleHeight;

    private int maxCol;
    private int maxRow;

    private int correct;
    private int moves;

    private int[][] layout;

    private Image[] piece;

    private Image imageBuffer;
    private Graphics graphicsBuffer;

    public void init()
    {
    moves = correct = 0;

    MediaTracker mt;

    Image source = getImage( getDocumentBase(), getParameter( "mario.jpg" ) );

    mt = new MediaTracker( this );
    mt.addImage( source, 0 );
    try { mt.waitForID( 0 ); } catch( InterruptedException e ){}

    debug( "Calculating dimensions..." );
    puzzleWidth = source.getWidth( this );
    puzzleHeight = source.getHeight( this );

    debug( "Image is " + puzzleWidth + " by " + puzzleHeight );

    maxCol = ( puzzleWidth / pref ) + 1;
    maxRow = ( puzzleHeight / pref ) + 1;

    debug( "Optimal grid is " + maxCol + " by " + maxRow );
    layout = new int[ maxRow ][ maxCol ];
    int len = maxCol * maxRow;

    piece = new Image[ len ];
    pieceWidth = puzzleWidth / maxCol;
    pieceHeight = puzzleHeight / maxRow;

    debug( "Each piece will be " + pieceWidth
    + " by " + pieceHeight + " pixels" );
    int mpw = maxCol * pieceWidth;
    int mph = maxRow * pieceHeight;

    imageBuffer = createImage( mpw, mph );
    graphicsBuffer = imageBuffer.getGraphics();

    debug( "Extracting pieces..." );
    mt = new MediaTracker( this );
    int row, col;
    for( row = 0; row < maxRow; row++ )
    {
    for( col = 0; col < maxCol; col++ )
    {
    CropImageFilter cif = new CropImageFilter( col * pieceWidth,
    row * pieceHeight,
    pieceWidth,
    pieceHeight );
    ImageProducer ip = source.getSource();
    ip = new FilteredImageSource( ip, cif );
    int num = ( row * maxCol ) + col;
    layout[ row ][ col ] = -1;
    piece[ num ] = createImage( ip );
    mt.addImage( piece[ num ], 0 );
    try { mt.waitForID( 0 ); } catch( InterruptedException e ){}
    }
    }
    debug( "Randomizing layout..." );
    row = col = 0;
    Random r = new Random();
    r.setSeed( System.currentTimeMillis() );

    // TODO: find a better [faster] randomization method!
    for( int i = 0; i < len; i++ )
    {
    col = Math.abs( r.nextInt() % maxCol );
    row = Math.abs( r.nextInt() % maxRow );

    while( layout[ row ][ col ] != -1 )
    {
    col += 1;

    if( col >= maxCol )
    {
    row += 1;
    row %= maxRow;
    col = 0;
    }
    }

    layout[ row ][ col ] = i;

    if( ( ( row * maxCol ) + col ) == i )
    {
    correct += 1;
    }
    }
    repaint();
    }

    public void paint( Graphics g )
    {
    int pwb = pieceWidth;
    int phb = pieceHeight;

    debug( "Drawing the screen..." );
    for( int row = 0; row < maxRow; row++ )
    {
    for( int col = 0; col < maxCol; col++ )
    {
    graphicsBuffer.drawImage( piece[ layout[ row ][ col ] ],
    col * pwb,
    row * phb,
    this );
    }
    }
    if( secondClick )
    {
    graphicsBuffer.setColor( Color.red );
    graphicsBuffer.drawRect( firstCol * pwb,
    firstRow * phb,
    pieceWidth - 1,
    pieceHeight - 1 );
    graphicsBuffer.drawRect( firstCol * pwb + 2,
    firstRow * phb + 2,
    pieceWidth - 5,
    pieceHeight - 5 );
    graphicsBuffer.setColor( Color.pink );
    graphicsBuffer.drawRect( firstCol * pwb + 1,
    firstRow * phb + 1,
    pieceWidth - 3,
    pieceHeight - 3 );
    }
    g.drawImage( imageBuffer, 0, 0, this );
    }

    public boolean mouseDown( Event e, int a, int b )
    {
    int col = ( a / pieceWidth );
    int row = ( b / pieceHeight );

    if( ( col < maxCol ) && ( row < maxRow ) )
    {
    debug( "User clicked on piece " + col + ", " + row );

    if( layout[ row ][ col ] == ( ( row * maxCol ) + col ) )
    {
    // beep?
    }
    else
    {
    if( secondClick )
    {
    int secondPiece = layout[ row ][ col ];
    int firstPiece = layout[ firstRow ][ firstCol ];

    debug( "Swapping piece " + secondPiece
    + " with " + firstPiece );

    layout[ firstRow ][ firstCol ] = secondPiece;
    layout[ row ][ col ] = firstPiece;

    secondClick = false;

    if( ( ( row * maxCol ) + col ) == firstPiece )
    {
    correct += 1;
    }

    if( ( ( firstRow * maxCol ) + firstCol ) == secondPiece )
    {
    correct += 1;
    }

    if( firstPiece != secondPiece )
    {
    moves += 1;
    }
    }
    else
    {
    firstRow = row;
    firstCol = col;
    secondClick = true;
    }
    repaint();
    }
    return true;
    }
    else
    {
    return false;
    }
    }

    private void debug( String s )
    {
    if( debug )
    {
    System.out.println( "[Puzzle] " + s );
    }
    }


  2. #2
    Junior Member
    Join Date
    Jun 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Please Help Me With This Error!

    First of all, a NullPointerException occurs when an object is Null, meaning it has no value, and you are trying to do something with it. Since the error occurs at line 57, there is something wrong with this line of code:

    debug( "Optimal grid is " + maxCol + " by " + maxRow );

    So, let's go through a checklist to pinpoint the problem.
    1) Does the debug method work as intended in the previous calls (For example, line 52?)

    --> If it works as intended in line 52, then there is something wrong with the following operations

    maxCol = ( puzzleWidth / pref ) + 1;
    maxRow = ( puzzleHeight / pref ) + 1;

    And since the code on line 52 works properly, this means that the pref variable is probably not initialized. However, pref is a final variable and is initialized, so the problem wouldn't be with the pref variable.

    What line does your IDE highlight when it shows you the error? What is the code of that line?

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Please Help Me With This Error!

    Something must be null.

    Either one, or both, of your two variables must be null. Why it's doing that is a mystery, as it should let you pass a null value as a param String. However, maybe you do something else with the String that is causing the error.

    Use try/catch blocks on the two variables used in the debug() call and have it pirnt out something for each to see which, if either, is null.

  4. #4
    Member
    Join Date
    Apr 2012
    Posts
    160
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: Please Help Me With This Error!

    It would help out a whole lot if you actually pointed us to what line 57 is

  5. #5
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Please Help Me With This Error!

    Quote Originally Posted by Parranoia View Post
    It would help out a whole lot if you actually pointed us to what line 57 is
    It appears to be:

    debug( "Optimal grid is " + maxCol + " by " + maxRow );

    if you copy and paste it directly into there.

  6. #6
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Please Help Me With This Error!

    Quote Originally Posted by Parranoia View Post
    It would help out a whole lot if you actually pointed us to what line 57 is


    Quote Originally Posted by iMofor
     
    import java.applet.Applet;
    import java.awt.*;
    import java.awt.image.*;
    import java.util.*;
     
    public class Puzzle extends Applet
    {
    private final boolean debug = false;
     
    private boolean secondClick = false;
     
    private final int pref = 72; // prefered tile size
     
    private int firstCol = -1;
    private int firstRow = -1;
     
    private int pieceWidth;
    private int pieceHeight;
     
    private int puzzleWidth;
    private int puzzleHeight;
     
    private int maxCol;
    private int maxRow;
     
    private int correct;
    private int moves;
     
    private int[][] layout;
     
    private Image[] piece;
     
    private Image imageBuffer;
    private Graphics graphicsBuffer;
     
    public void init()
    {
    moves = correct = 0;
     
    MediaTracker mt;
     
    Image source = getImage( getDocumentBase(), getParameter( "mario.jpg" ) );
     
    mt = new MediaTracker( this );
    mt.addImage( source, 0 );
    try { mt.waitForID( 0 ); } catch( InterruptedException e ){}
     
    debug( "Calculating dimensions..." );
    puzzleWidth = source.getWidth( this );
    puzzleHeight = source.getHeight( this );
     
    debug( "Image is " + puzzleWidth + " by " + puzzleHeight );
     
    maxCol = ( puzzleWidth / pref ) + 1;
    maxRow = ( puzzleHeight / pref ) + 1;
     
    debug( "Optimal grid is " + maxCol + " by " + maxRow );
    layout = new int[ maxRow ][ maxCol ];
    int len = maxCol * maxRow;
     
    piece = new Image[ len ];
    pieceWidth = puzzleWidth / maxCol;
    pieceHeight = puzzleHeight / maxRow;
     
    debug( "Each piece will be " + pieceWidth
    + " by " + pieceHeight + " pixels" );
    int mpw = maxCol * pieceWidth;
    int mph = maxRow * pieceHeight;
     
    imageBuffer = createImage( mpw, mph );
    graphicsBuffer = imageBuffer.getGraphics();
     
    debug( "Extracting pieces..." );
    mt = new MediaTracker( this );
    int row, col;
    for( row = 0; row < maxRow; row++ )
    {
    for( col = 0; col < maxCol; col++ )
    {
    CropImageFilter cif = new CropImageFilter( col * pieceWidth,
    row * pieceHeight,
    pieceWidth,
    pieceHeight );
    ImageProducer ip = source.getSource();
    ip = new FilteredImageSource( ip, cif );
    int num = ( row * maxCol ) + col;
    layout[ row ][ col ] = -1;
    piece[ num ] = createImage( ip );
    mt.addImage( piece[ num ], 0 );
    try { mt.waitForID( 0 ); } catch( InterruptedException e ){}
    }
    }
    debug( "Randomizing layout..." );
    row = col = 0;
    Random r = new Random();
    r.setSeed( System.currentTimeMillis() );
     
    // TODO: find a better [faster] randomization method!
    for( int i = 0; i < len; i++ )
    {
    col = Math.abs( r.nextInt() % maxCol );
    row = Math.abs( r.nextInt() % maxRow );
     
    while( layout[ row ][ col ] != -1 )
    {
    col += 1;
     
    if( col >= maxCol )
    {
    row += 1;
    row %= maxRow;
    col = 0;
    }
    }
     
    layout[ row ][ col ] = i;
     
    if( ( ( row * maxCol ) + col ) == i )
    {
    correct += 1;
    }
    }
    repaint();
    }
     
    public void paint( Graphics g )
    {
    int pwb = pieceWidth;
    int phb = pieceHeight;
     
    debug( "Drawing the screen..." );
    for( int row = 0; row < maxRow; row++ )
    {
    for( int col = 0; col < maxCol; col++ )
    {
    graphicsBuffer.drawImage( piece[ layout[ row ][ col ] ],
    col * pwb,
    row * phb,
    this );
    }
    }
    if( secondClick )
    {
    graphicsBuffer.setColor( Color.red );
    graphicsBuffer.drawRect( firstCol * pwb,
    firstRow * phb,
    pieceWidth - 1,
    pieceHeight - 1 );
    graphicsBuffer.drawRect( firstCol * pwb + 2,
    firstRow * phb + 2,
    pieceWidth - 5,
    pieceHeight - 5 );
    graphicsBuffer.setColor( Color.pink );
    graphicsBuffer.drawRect( firstCol * pwb + 1,
    firstRow * phb + 1,
    pieceWidth - 3,
    pieceHeight - 3 );
    }
    g.drawImage( imageBuffer, 0, 0, this );
    }
     
    public boolean mouseDown( Event e, int a, int b )
    {
    int col = ( a / pieceWidth );
    int row = ( b / pieceHeight );
     
    if( ( col < maxCol ) && ( row < maxRow ) )
    {
    debug( "User clicked on piece " + col + ", " + row );
     
    if( layout[ row ][ col ] == ( ( row * maxCol ) + col ) )
    {
    // beep?
    }
    else
    {
    if( secondClick )
    {
    int secondPiece = layout[ row ][ col ];
    int firstPiece = layout[ firstRow ][ firstCol ];
     
    debug( "Swapping piece " + secondPiece
    + " with " + firstPiece );
     
    layout[ firstRow ][ firstCol ] = secondPiece;
    layout[ row ][ col ] = firstPiece;
     
    secondClick = false;
     
    if( ( ( row * maxCol ) + col ) == firstPiece )
    {
    correct += 1;
    }
     
    if( ( ( firstRow * maxCol ) + firstCol ) == secondPiece )
    {
    correct += 1;
    }
     
    if( firstPiece != secondPiece )
    {
    moves += 1;
    }
    }
    else
    {
    firstRow = row;
    firstCol = col;
    secondClick = true;
    }
    repaint();
    }
    return true;
    }
    else
    {
    return false;
    }
    }
     
    private void debug( String s )
    {
    if( debug )
    {
    System.out.println( "[Puzzle] " + s );
    }
    } 
    }


    I'm getting a NPE at line 50, with direct cut and paste, er, after I put in the extra } to make it compile.

    Somehow I'm wondering if the variable source is null.

    Yep, a simple try catch found that the variable source is indeed null.

    At least for me. I admit I don't have that image, but is that where you're getting the error as well?

    It is in the 50s so it's really close to where you're at.
    Last edited by javapenguin; June 3rd, 2012 at 11:50 PM. Reason: Yes, the variable source is null.

  7. #7
    Junior Member
    Join Date
    May 2012
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Please Help Me With This Error!

    Id like to start off by thanking everyone for their time in helping me. The line that is highlighted when im attempting to run is:

    puzzleWidth = source.getWidth (this); (Line 55)

  8. #8
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Please Help Me With This Error!

    Check and see if source is null.

  9. #9
    Junior Member
    Join Date
    May 2012
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Please Help Me With This Error!

    I don't exactly understand what you mean. If you mean that if the source height and width of the image are coming up as null, then yes the source is null.

  10. #10
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Please Help Me With This Error!

    I meant the variable source. Perhaps you don't have it in a folder that it can access or it can't find the file.

    If it can't find the file, it can't get a height or width for the image that it can't find.

    Before that line that causes the exception, add this

    System.out.println("The variable source is null: " + source == null);

    If it says

    "The variable source is null:true" then the culprit might be here:

    Image source = getImage( getDocumentBase(), getParameter( "mario.jpg" ) );

    If it says false, then it's something else.
    Last edited by javapenguin; June 4th, 2012 at 08:24 PM.

  11. The Following User Says Thank You to javapenguin For This Useful Post:

    iMofro (June 12th, 2012)

  12. #11
    Junior Member
    Join Date
    May 2012
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Please Help Me With This Error!

    Thank you for your time and help, I tried your idea and its coming up as "False" which means that it must be something else. Do you have any ideas of what else it could possibly be?

    So basically the error that I am getting is saying that the images height and width cannot be determined. (From my understanding)
    Last edited by iMofro; June 5th, 2012 at 09:25 AM.

  13. #12
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Please Help Me With This Error!

    Just to be sure, try calling some other Image method and see if that too throws a NullPointerException.

    As for the width and height, even if it wasn't able to figure it out, it should still return -1.
    Last edited by javapenguin; June 5th, 2012 at 01:09 PM.

  14. #13
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Please Help Me With This Error!

    I tried your idea and its coming up as "False" which means that it must be something else. Do you have any ideas of what else it could possibly be?
    First, please use the code tags to maintain code format. Second, please post an SSCCE - about 99% of the code you posted has nothing to do with the problem. Third - as mentioned time and again, something is null on the line the error states. Use some println's to see what it is (most likely your image), then work backwards from there to see how it came to be null.

  15. #14
    Junior Member
    Join Date
    May 2012
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Please Help Me With This Error!

    Thanks a lot! We got the programming working