Results 1 to 10 of 10
  1. #1
    tommyyyy is offline Member
    Join Date
    Mar 2009
    Posts
    10
    Rep Power
    0

    Default NullPointerException

    //code
    public static void printdata(Object[] mpdata)
    {
    int m=0;
    while ((mpdata[m] != null)){
    for (int p=0; p < mpdata.length; p++){ //the error is here

    System.out.println(mpdata[p]);
    }
    m++;}
    }
    //code

    Does anybody can tell me how to correct this error

  2. #2
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    Perhaps some array components are not initiated. It is al depends of the method param content. What and how uses the method...

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    You check the null value only for the first element of the array. What happen if the second element is null. You get the NullPointerException. That's what happen in your code.

  4. #4
    tommyyyy is offline Member
    Join Date
    Mar 2009
    Posts
    10
    Rep Power
    0

    Default

    Eranga, there is m++, does it work

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  6. #6
    azzaiel's Avatar
    azzaiel is offline Member
    Join Date
    Feb 2009
    Posts
    57
    Rep Power
    0

    Default

    i think there's a problem on or while loop
    for example your

    mpdata.lenght = 1 (modata[0] = "test")

    m = 0;

    the fist loop is ok but because m = 0 and address 0 is existing in your array

    then when it m = 1

    address 1 is not an exiting array
    It's easy to write a code that computers can understand...
    ... the challenge is to write a code that humans can understand

  7. #7
    tommyyyy is offline Member
    Join Date
    Mar 2009
    Posts
    10
    Rep Power
    0

    Default

    The full error is here

    Exception in thread "main" java.lang.NullPointerException
    at MPD.printdata(MobilePhoneDatabase.java:122)
    at MPD.main(MobilePhoneDatabase.java:167)

    Process completed.

    The line 122 is exactly the error resist, the for loop
    The line 167 is the main method call to printdata method

  8. #8
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    Have you checked to be sure that the array has data? Does anything get printed ? Does it fail on the first element?

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  9. #9
    softslate is offline Member
    Join Date
    Mar 2009
    Location
    Schenectady, NY
    Posts
    11
    Rep Power
    0

    Default

    The array itself may be null. Check that mpdata is not null.
    SoftSlate Commerce Java Shopping Cart
    www.softslate.com

  10. #10
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    try printing out values to the screen whenever you use them or use a debugger. it'll help you track down how the program strays from expected behavior

Similar Threads

  1. NullPointerException I NEED HELP
    By mayhewj7 in forum New To Java
    Replies: 2
    Last Post: 02-13-2009, 09:03 AM
  2. NullPointerException
    By adeeb in forum AWT / Swing
    Replies: 3
    Last Post: 06-11-2008, 09:42 AM
  3. NullPointerException
    By mensa in forum Java 2D
    Replies: 5
    Last Post: 05-04-2008, 12:19 AM
  4. NullPointerException
    By ravian in forum New To Java
    Replies: 2
    Last Post: 12-07-2007, 05:20 PM
  5. NullPointerException
    By Feng in forum New To Java
    Replies: 5
    Last Post: 11-24-2007, 08:51 PM

Posting Permissions

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