File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

string array in while loop not wirking

 
manohar gunturu
Greenhorn
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my code as given below

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.sql.*"%>
<%@page import="java.util.*"%>
<%@page import="java.io.*"%>

<%!
Connection conn=null;
ResultSet rs=null;
Statement stmt=null;
%>

<%
String email="mounika@yahoo.com";
String gunt="SELECT * from sony.frndreq where to='"+email+"' or from='"+email+"'";
String[] anArray=null;
int f=1;

try{
Class.forName("com.ibm.db2.jcc.DB2Driver");
conn=DriverManager.getConnection("jdbc:db2://localhost:50000/mail","db2admin","db2admin");

stmt=conn.createStatement();
rs=stmt.executeQuery(gunt);
while(rs.next()){
anArray[f]=rs.getString("from");
out.println(anArray[f]);

f++;
}
}
catch(SQLException e) {
out.println(e); }


%>


and error is as fallowing
org.apache.jasper.JasperException: An exception occurred processing JSP page /testing.jsp at line 25

22: stmt=conn.createStatement();
23: rs=stmt.executeQuery(gunt);
24: while(rs.next()){
25: anArray[f]=rs.getString("from");
26: out.println(anArray[f]);
27:
28: f++;


Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:521)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:430)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
root cause

java.lang.NullPointerException
org.apache.jsp.testing_jsp._jspService(testing_jsp.java:82)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:723)

please help of how to overcome this
 
manohar gunturu
Greenhorn
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok i got it the error is i did not initialized the string array properly.

Thank's
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64192
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you realize that by using scriptlet declarations that only one person can use your site at a time without conflict?

Why are you putting Java code into a JSP page in 2013 when doing so has been obsolete since 2002?
 
manohar gunturu
Greenhorn
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Do you realize that by using scriptlet declarations that only one person can use your site at a time without conflict?

Why are you putting Java code into a JSP page in 2013 when doing so has been obsolete since 2002?


Thank you .
Can you please tell to me how to write this code in a correct manner or how to solve the problem of only one person can use the site at a time with conflict.

Thanks for any help.
 
Ahsan Bagwan
Ranch Hand
Posts: 247
1
Java MySQL Database Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use of scriptlets here is frowned upon. The friendly folks here have compiled an article describing all the bad things that can happen when you use Scriptlets ( <- link )
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64192
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should be doing database access in a Java class, not a JSP. In a JSP, you should only be using the JSTL and EL; not Java scriptlets.

Please read this article for information on properly structuring web apps.
 
Ravi Khadgi
Greenhorn
Posts: 20
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As everyone suggested, you should not be writing any java code inside a JSP. The problem with your posted code is that you have not instantiated the String array variable "anArray".

String[] anArray=null;
....
....
anArray[f]=rs.getString("from"); (NullPointerException will be thrown here as anArray is null)
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic