Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

null value always returned!?!

Options
  • 22-03-2006 9:37pm
    #1
    Registered Users Posts: 1,987 ✭✭✭


    This is the code in a .class file and its called by this lline <%= user.name(username) %> i know that the username var is being passed in as i have other method calls working but the below method always returns a null value even though the data is in the DB!?!?
    public String name(String value)
    	{
    		this.value = value;
    		
    		connect();
    		
    		try
    		{
    			stmt = conn.createStatement();
    			rset = stmt.executeQuery("SELECT fname FROM username WHERE username = '"+value+"'");
    			if(rset.next())
    			{
    				value = rset.getString(1);
    			}
    		}
    		catch(SQLException e)
    		{
    			
    		}
    		closeConnect();
    		return value;
    	}
    


Comments

  • Closed Accounts Posts: 453 ✭✭nuttz


    first of all, try using the [code] tags when posting

    Have you tried debugging through the code?, step through it line by line, and inspect the objects as you go.


  • Registered Users Posts: 1,987 ✭✭✭Ziycon


    gone through the debuging but cant find out where or whats going wrong!


  • Registered Users Posts: 2,781 ✭✭✭amen


    have you run the sql statement and does it return data ?
    have you profiled what is going to the database?


  • Registered Users Posts: 1,481 ✭✭✭satchmo


    Put some code in the catch statement and see if it's throwing an exception.


  • Registered Users Posts: 20,994 ✭✭✭✭Stark


    Hmm my first guess would have been that you made a mistake in referring to your SQL table as "username" when "username" is a field within the table. The method would just throw a SQLException which you catch without printing. (You can put in e.printStackTrace() to print debugging information). That would lead to the line "value = rset.getString(1);" never getting called.

    But that would cause you to return the initial value which was passed into the method. (stick a System.out.println(value) statement at the start to make sure you aren't being passed a null value).

    The only other thing I can think of is that the relevant row in the database has fname set to NULL.


  • Advertisement
  • Registered Users Posts: 1,987 ✭✭✭Ziycon


    the prob is, i cant use console println state ments because its a jsp website thats using a complied .class file so i cant see the errors when im on the site, is there a way of outputting the errors from the .class file to the .jsp page?


  • Closed Accounts Posts: 453 ✭✭nuttz


    Ziycon wrote:
    the prob is, i cant use console println state ments because its a jsp website thats using a complied .class file so i cant see the errors when im on the site, is there a way of outputting the errors from the .class file to the .jsp page?

    Download eclipse and the myeclipse trial or bea workshop studio trial and use them to debug your JSPs, bea workshop studio would be my prefered option


  • Registered Users Posts: 1,987 ✭✭✭Ziycon


    i've downloaded eclipse and i've installed the latest sdk and jre but i get this error when i run the eclipse.exe:
    Required Java version: 1.4.1. Available 1.3.1_01.


  • Closed Accounts Posts: 17,208 ✭✭✭✭aidan_walsh


    Ziycon wrote:
    i've downloaded eclipse and i've installed the latest sdk and jre but i get this error when i run the eclipse.exe:
    You'll be wanting to edit the classpath environment variable for that.


  • Registered Users Posts: 1,987 ✭✭✭Ziycon


    nice one aidan_walsh, had to change the classpath, got eclipse working now!:D


  • Advertisement
  • Registered Users Posts: 20,994 ✭✭✭✭Stark


    Ziycon wrote:
    the prob is, i cant use console println state ments because its a jsp website thats using a complied .class file so i cant see the errors when im on the site, is there a way of outputting the errors from the .class file to the .jsp page?

    Woops, my bad. There might be Systemout.log files on the server though if you can get access to them.


  • Registered Users Posts: 4,003 ✭✭✭rsynnott


    I realise that at some point someone's just going to come and shoot me when I say this, but I really can't bear to see this nonsense. Google SQL Injection, immediately.


  • Registered Users Posts: 1,987 ✭✭✭Ziycon


    Ok, i managed to get to the bottom of the problem, its nothin to do with the java code in the complied .class file, the session variable is assigning itself to null whne it goes to the next page but the session is still alive!


  • Registered Users Posts: 1,987 ✭✭✭Ziycon


    Th value is lost when your either coming from the login page or the register page, both these pages lead to the user page wit the below code belongs to!
    Just cant figure it out!?!:confused:

    This is the code in the top of the page.
    <%@page import="java.util.*" session="true"%>
    <%
      String username=(String) session.getAttribute("username");
      if(username=="") username=null;
    %>
    

    Both these lines are returning null so its the session variable 'username' thats losing its value from the previous page.
    <%= username %>
    <%= user.name(username) %>
    


  • Registered Users Posts: 130 ✭✭irishfeller


    The line of code if(username=="") should be if(username.equals(("")) as its a string not a char. Not sure if this would cause your problem tho.

    Try printing the session attribute directly to screen to see if it is null:
    <%=(String) session.getAttribute("username")%>

    Also check that session=true on the JSP where you set the attribute.

    What type of app server are u using? They all have logs for catching system.outs.


Advertisement