Results 1 to 6 of 6
  1. #1
    jayragz is offline Member
    Join Date
    Apr 2011
    Posts
    35
    Rep Power
    0

    Default NullPointerException

    Hi,
    Can anyone explain to me what "NullPointerException" means Ive thoroughly gone through my code and don't see any errors but after executing one function of my program it gives me 'Exception in thread "main" java.lang.NullPointerException'.

    I need help ASAP,
    Thanx in advance.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Null pointer means you are trying to do something to an uninitialized object. This quick example will generate one
    Java Code:
    public class Null{
      public static void main(String[] args){
        Object o;
        o.toString();
      }
    }
    The reason this causes an error is because the object o doesn't refer to anything, it's simple a reference. Calling toString on it causes a null pointer exception since o doesn't point(refer) at anything

    For more help please post the pertinent code and EXACT error message(copy/paste). Post code in code tags.

    [code]
    YOUR CODE HERE
    [/code]

  3. #3
    jayragz is offline Member
    Join Date
    Apr 2011
    Posts
    35
    Rep Power
    0

    Default

    this method executes the deleting of an address entry from an address book that has been persisted
    Java Code:
    public void execute() throws ActionException {
    
            DeleteAddressActionArgument deleteAddressArgument =
                ( DeleteAddressActionArgument )actionArgument;
    
            String name = deleteAddressArgument.getName();
    
            try {
                AddressBookEntry addressBookEntry =
                addressBookDelegate.getAddressBookEntry( name );
    
                addressBookDelegate.deleteAddressBookEntry( addressBookEntry);
            }
            catch( AddressBookDelegateException e ) {
    
                throw new ActionException(
                        "Could not delete Address book entry for, " + name, e );
            }

    and it gets its the address from the following method
    Java Code:
    public AddressBookEntry getAddressBookEntry( String name )
            throws AddressBookDelegateException {
    
            String nameWithUnderscores = replaceSpacesWithUnderScores( name );
    
            File fileToUnserialize =
                new File( addressBookDirectory, nameWithUnderscores );
    
            if ( ! fileToUnserialize.exists() ) {
    
                return null;
            }
    
            try {
                InputStream inputStream = new FileInputStream( fileToUnserialize );
    
                ObjectInputStream objectInputStream
                    = new ObjectInputStream( inputStream );
    
                AddressBookEntry addressBookEntry =
                    ( AddressBookEntry )objectInputStream.readObject();
    
                return addressBookEntry;
            }
            catch( FileNotFoundException e ) {
    
                throw new AddressBookDelegateException(
                        "Could not find the following file to deserialize, " +
                            fileToUnserialize.getAbsolutePath(), e );
            }
            catch( IOException e ) {
    
                throw new AddressBookDelegateException(
                        "Could not read inputStream of file, " +
                            fileToUnserialize.getAbsolutePath(), e );
            }
            catch( ClassNotFoundException e ) {
    
                throw new AddressBookDelegateException(
                        "Could not find class, " + AddressBookEntry.class +
                            " to desrialize file, " +
                                fileToUnserialize.getAbsolutePath(), e );
            }
        }
    the two methods only tell like 1/3 of the story since there is a whole lot of code involved.
    s it possible for you to derive anything from the above methods?

  4. #4
    jayragz is offline Member
    Join Date
    Apr 2011
    Posts
    35
    Rep Power
    0

    Default

    Thanx i found out where the problem was..

  5. #5
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    Quote Originally Posted by sunde887 View Post
    Java Code:
    public class Null{
      public static void main(String[] args){
        Object o;
        o.toString();
      }
    }
    Actually, that won't compile.
    Either:
    Java Code:
    Object o = null;
    or move o to be a static attribute of the class.

  6. #6
    jayragz is offline Member
    Join Date
    Apr 2011
    Posts
    35
    Rep Power
    0

Similar Threads

  1. NullPointerException
    By sanu in forum Java Applets
    Replies: 3
    Last Post: 08-21-2010, 08:37 PM
  2. NullPointerException
    By speedzojie@gmail.com in forum New To Java
    Replies: 5
    Last Post: 06-03-2010, 05:39 PM
  3. NullPointerException
    By donchini in forum New To Java
    Replies: 4
    Last Post: 05-20-2010, 01:11 AM
  4. NullPointerException
    By mjz in forum JDBC
    Replies: 1
    Last Post: 08-06-2009, 11:46 AM
  5. NullPointerException I NEED HELP
    By mayhewj7 in forum New To Java
    Replies: 2
    Last Post: 02-13-2009, 08:03 AM

Posting Permissions

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