Pregunta

I am working an a small database assignment, and I seem to get a weird nullPointerException where I cannot find the problem

The following snippet is the error:

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at interfaces.FindDueActivityForFriend$1.actionPerformed(FindDueActivityForFriend.java:65)
at java.awt.Button.processActionEvent(Unknown Source)
at java.awt.Button.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

As you can see the problem occurs in the 65 of the FindDueActivityForFriend class, and to be more specific in this code snippet:

    friendSearch = friendSearchField.getText();
            console.out("Searching for the name that was entered");
            console.out("friendSearch variable is: " + friendSearch );

            if(con.GetNameExcists(friendSearch )){
//more code

the error occurs at the if statement. In this if statement a class is called that connects to the database, checks if a name is already in the database, and then returns a true if that name is already in the database.

I have used that specific method around 10 times around the program without problems, but not this time.

the console.out("String"); that I am calling just before it is for a small console windows that I made that just prints what I tell it to, this is to have a console when not running the application out of for example eclipse.

I tried printing the variable just to see what it was, and it is not null. following is a copy paste of the console:

    [Wed May 14 20:04:26 CEST 2014] [INFO] Opened the findDueForFriend screen
    [Wed May 14 20:04:30 CEST 2014] [INFO] Searching for the name that was entered
    [Wed May 14 20:04:30 CEST 2014] [INFO] FriendSearch variable is: Ylva

I went on into ant debugger, and is doesn't even go in the method, it throws the nullpointer before actually opening the method

I don't have a clue what I am doing wrong anymore so if anyone has any suggestions please post them.

just for completeness here is the declaration of the variable aswell:

    String friendSearch;

public FindDueActivityForFriend(Console Console){
    console = Console;
    friendSearch = "";

I even tried declaring the String as an empty string, not sure if that makes it not null but I think it should, and the console window clearly shows that the string contains something before throwing the nullpointer exception...

¿Fue útil?

Solución

If it occurs in this if statement

if(con.GetNameExcists(friendSearch ))

and you know that friendSearch is not null, then con must be null, in order to get a NullPointerException in the if statement.

Otros consejos

the only possibility with the below code is

 if(con.GetNameExcists(friendSearch ))

either con is null or friendSearch is null which you might be accessing at the

begining of the method and performing some operation like friendSearch.caoncat or something.

mostly it should be con is null it seems.

try to print con value inside log.

if con.getNameExcists(...) return Boolean instead of boolean and possibly returns null, then you get a NullPointerException from auto-unboxing.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top