Class not returning all Values of Array

When I send an array with two elements to this class:

publicclass DrawingList{

public String[] dwglist;

public DrawingList(String[] list){

this.dwglist = list;

}

public String[] getDwgList(){

return this.dwglist;

}

publicstatic DrawingList getList(String[] list){

returnnew DrawingList(list);

}

}

It only returns the last element.

Can some one help me with this issue please.

[1011 byte] By [SLDykea] at [2007-11-26 22:41:34]
# 1
send how? returns where? how do you know it's only the last element?
georgemca at 2007-7-10 11:56:33 > top of Java-index,Java Essentials,New To Java...
# 2

Hi,

from what you write I guess you have written some piece of code that calls getList() with an String[] containing 2 elements and then written some code calling getDwgList().

I think that you have made in error in the latter part mentioned. Please be so kind as to post these pieces of code as well.

g_magossa at 2007-7-10 11:56:33 > top of Java-index,Java Essentials,New To Java...
# 3

[nobr]This is the class that generates the initial array which contains two elements:

import java.sql.*;

import com.ibm.as400.access.*;

/**

* @author sde

*

* TODO To change the template for this generated type comment go to

* Window - Preferences - Java - Code Style - Code Templates

*/

public class DrawingConnectionBaseClass {

public String nameofdrawingb;

public String errorStringb;

public String literalerrorStringb;

public String[] dwgNoArray;

public DrawingConnectionBaseClass(String arg0) {

AS400JDBCDataSource datasource = new AS400JDBCDataSource("gvas400");

datasource.setUser("drawchg");

datasource.setPassword("webabc1");

try

{

Connection connection = datasource.getConnection();

Statement stmt = connection.createStatement();

String sqla = "select Count(*) As dwgcount from webprddt6.drawmstrp Where dm_drawing_number Like '%" + arg0 + "%'" ;

String sql = "select * from webprddt6.drawmstrp Where dm_drawing_number Like '%" + arg0 + "%'" ;

ResultSet rsa = stmt.executeQuery(sqla);

rsa.next();

int rowCount = rsa.getInt(1);

dwgNoArray = new String[rowCount];

errorStringb = "";

ResultSet rs = stmt.executeQuery(sql);

while (rs.next())

{

for (int i = 0; i < rowCount - 1; ++i)

{

dwgNoArray[i] = rs.getString("DM_DRAWING_NUMBER");

}

}

errorStringb = "";

literalerrorStringb = "NoError";

}

catch (SQLException sqle)

{

errorStringb = "Warning!!! The Drawing Number you typed is not valid!";

literalerrorStringb = "Error";

}

}

}

This is a servlet that calls the above class and then the DrawingList class to create a bean to be used on a JSP:

import java.io.IOException;

import javax.servlet.RequestDispatcher;

import javax.servlet.Servlet;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;

import java.sql.*;

public class DrawingInquiryBaseServlet extends HttpServlet implements Servlet {

/* (non-Java-doc)

* @see javax.servlet.http.HttpServlet#HttpServlet()

*/

public DrawingInquiryBaseServlet() {

super();

}

public static String getliteralErrStringb;

public static String getErrMsgb;

public static String[] drawingList;

/* (non-Java-doc)

* @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest arg0, HttpServletResponse arg1)

*/

protected void doGet(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {

this.doPost(arg0, arg1);

}

/* (non-Java-doc)

* @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest arg0, HttpServletResponse arg1)

*/

protected void doPost(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {

String drawingnumber = arg0.getParameter("dwgnobase");

DrawingConnectionBaseClass conn = new DrawingConnectionBaseClass(drawingnumber);

DrawingList drawingList = DrawingList.getList(conn.dwgNoArray);

ErrorHandling getErrMsgb = ErrorHandling.getErr(conn.errorStringb);

getliteralErrStringb = conn.literalerrorStringb;

HttpSession session = arg0.getSession(true);

if (getliteralErrStringb.equals("NoError")){

session.setAttribute("errorBean", getErrMsgb);

session.setAttribute("dwglist", drawingList);

}

else{

session.setAttribute("errorBean", getErrMsgb);

session.setAttribute("dwglist", drawingList);

}

RequestDispatcher disp = getServletContext().getRequestDispatcher("/DrawingDisplaybase.jsp");

disp.forward(arg0, arg1);

}

}

This is my JSP where only one element is in the list:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<HTML>

<HEAD>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"

pageEncoding="ISO-8859-1"%>

<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<META name="GENERATOR" content="IBM Software Development Platform">

<META http-equiv="Content-Style-Type" content="text/css">

<LINK href="theme/Master.css" rel="stylesheet"

type="text/css">

<TITLE>DrawingDisplay2.jsp</TITLE>

</HEAD>

<BODY>

<P><IMG border="0" src="Zodiac Z Blue (Hi Res).gif" width="73"

height="76"><B><B><FONT face="Times New Roman" color="navy" size="+2"> WEBER

AIRCRAFT LP</FONT></B></B><BR>

<BR>

<B><FONT size="+1" face="Times New Roman" color="navy"> Drawing Inquiry

Data</FONT></B></P>

<HR>

<P><B><FONT size="+2" color="red"><jsp:useBean id="errorBean"

type="com.ibm.drawinginquiry.rational.ErrorHandling" scope="session"></jsp:useBean>

<jsp:getProperty name="errorBean" property="errMsg" /></FONT></B></P>

<P>

Pick a Drawing number from list to see Details<BR>

<jsp:useBean id="dwglist"

type="com.ibm.drawinginquiry.rational.DrawingList" scope="session"></jsp:useBean><BR><SELECT size="5" name="dwgList">

<%

for (int i=0; i<dwglist.dwglist.length; i++){

%>

<option value="<%= i%>"><%= dwglist.dwglist[i]%></option>

<%

}

%>

</SELECT></P>

<P><BR>

<BR>

<BR>

</P>

<FORM action="/DrawingInquiry/GetMainPageServlet"><INPUT type="submit"

name="anotherDrawing" value="Look Up Another Drawing"></FORM>

</BODY>

</HTML>

[/nobr]

SLDykea at 2007-7-10 11:56:33 > top of Java-index,Java Essentials,New To Java...
# 4

hi,

first of all I must excuse myself I am not a native English speaker nor am I living in an english speaking country, so my understanding is limited and my english might sound clumpsy.

I had a look at your code. Spefically at this part

Connection connection = datasource.getConnection();

Statement stmt = connection.createStatement();

String sqla = "select Count(*) As dwgcount from webprddt6.drawmstrp Where dm_drawing_number Like '%" + arg0 + "%'" ;

String sql = "select * from webprddt6.drawmstrp Where dm_drawing_number Like '%" + arg0 + "%'" ;

ResultSet rsa = stmt.executeQuery(sqla);

rsa.next();

int rowCount = rsa.getInt(1);

dwgNoArray = new String[rowCount];

errorStringb = "";

ResultSet rs = stmt.executeQuery(sql);

while (rs.next())

{

for (int i = 0; i < rowCount - 1; ++i)

{

dwgNoArray[i] = rs.getString("DM_DRAWING_NUMBER");

}

}

errorStringb = "";

literalerrorStringb = "NoError";

}

catch (SQLException sqle)

I think what you wanted to do is retrieving the number of records that will come back in the result set, declare an array of that size and than load the value of the column DM_DRAWIN_NUMBER of each record into the respective position of this array.

this would be done by :

int i = 0 ;

while (rs.next()) {

dwgNoArray[i] = rs.getString("DM_DRAWING_NUMBER");

i=i+1 ;

}

what you do instead is, you parse through the result set (while (rs.next()) and than in your for loop you set every value in the array dwgNoArray to the value of DM_DRAWING_NUMBER of the current record set.

You will end up with an array dwgNoArray that has the last value of DM_DRAWING_NUMBER in every place.

Hope you understand what I mean.

g_magossa at 2007-7-10 11:56:33 > top of Java-index,Java Essentials,New To Java...
# 5

Some remarks on class design (this post won't answer your question...):

Why is dwgList a public member of your class? You should consider making it private, otherwise the get and set methods are superfluous. I would also consider creating a safety copy of the array when storing it to dwgList.

ProggerFromKupfera at 2007-7-10 11:56:33 > top of Java-index,Java Essentials,New To Java...
# 6
I don't see anything wrong with your English. I thank you for your help in this issue. Your suggestion worked. I am just staring Java development and I will have a lot of questions.
SLDykea at 2007-7-10 11:56:33 > top of Java-index,Java Essentials,New To Java...
# 7
What do you mean by safety copy?
SLDykea at 2007-7-10 11:56:33 > top of Java-index,Java Essentials,New To Java...
# 8
If I make dwgList private my app will not work. It throws errors.
SLDykea at 2007-7-10 11:56:33 > top of Java-index,Java Essentials,New To Java...
# 9

> If I make dwgList private my app will not work. It

> throws errors.

Change this code in the jsp:

for (int i=0; i<dwglist.dwglist.length; i++){

// It should use the getter instead of directly accessing the dwglist, like this

String [] drawingList = dwgList.getDwglist();

for (int i=0; i><drawingList.length; i++){

%>

<option value="<%= i%>"><%= drawingList[i]%></option>

<%

}

%>

Message was edited by:

SomeoneElse

Message was edited by:

SomeoneElse

SomeoneElsea at 2007-7-10 11:56:33 > top of Java-index,Java Essentials,New To Java...
# 10
I am still having trouble with:<option value="<%= i%>"><%= dwglist.dwglist[i]%></option>If I set the dwglist as private.
SLDykea at 2007-7-10 11:56:33 > top of Java-index,Java Essentials,New To Java...
# 11
Did you read my edited reply? Read it again. You create a new String[], and assign it the value of dwglist.getDwgList(), then use that to fill the options.
SomeoneElsea at 2007-7-10 11:56:33 > top of Java-index,Java Essentials,New To Java...