Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Wrong number of arguments in constructor. Anyone to the rescue please?

Status
Not open for further replies.

kilo2

Programmer
Jan 2, 2003
3
GB
Everytime I run the jsp beow I get the following error can anybody help me


Generated servlet error:
C:\Tomcat\jakarta-tomcat-4.0.6\work\Standalone\localhost\_\shop_0002dproducts$jsp.java:188: Wrong number of arguments in constructor.
Product item = new Product(item_id, title, price);
^
1 error, 1 warning


The code

<%@ page language=&quot;java&quot; contentType=&quot;text/html&quot; import=&quot;ShoppingBasket
,Product, java.sql.*, java.util.*&quot; errorPage=&quot;errorpage.jsp&quot; %>

<html><head><title> Welcome to the Shop </title></head>

<body >

<table width=&quot;385&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; >

<tr> <td colspan=&quot;4&quot;>Roberts Record Store products:</td> </tr>
<tr> <td colspan=&quot;4&quot; align=&quot;right&quot;>

//hyperlink to view basket

<a href=&quot;<%= response.encodeURL(&quot;shop-basket.jsp&quot;) %>&quot;>
<img src=&quot;images\viewbasket.gif&quot;></a></td>

</tr>

<tr><td><b>Ref</b></td> <td><b>Title</b></td>
<td><b>Price</b></td> <td></td> </tr>

//get records from the items table in the shop database
<%
//1. Load the mySql jdbc driver

Class.forName(&quot;org.gjt.mm.mysql.Driver&quot;);

//2. open a connection to the &quot;shop&quot; database
java.sql.Connection connection = java.sql.DriverManager.getConnection
(&quot;jdbc:mysql://localhost/shop&quot;);

//3. create a statement object for sending SQL queries
java.sql.Statement stmt=connection.createStatement();

//4. place query results in a ResultSet object
java.sql.ResultSet RS = stmt.executeQuery(&quot;SELECT * FROM items&quot;);

//write a HTML table containing each record and assign ResultSet's column 1 to a string variable

int rowCounter = 0;
while (RS.next()) {

String item_id = RS.getString(&quot;item_id&quot;);
String title = RS.getString(&quot;title&quot;);
String description = RS.getString(&quot;description&quot;);
String price = RS.getString(&quot;price&quot;);
rowCounter++;

String bg = (rowCounter %2 !=0) ?&quot;#C0C0C0&quot;:&quot;#FFFFFF&quot;;


%>

<tr bgcolor=&quot;<%= bg %>&quot;>
<td><%= item_id %><td/>
<td><b><%= title %></b><br/>
<%= description %></td>
<td>£<%= price %></td>

<td>

//hyperlink to add an item to the basket


<a href=&quot;<%= response.encodeURL (&quot;shop-products.jsp?title=&quot;+title+&quot;&item_id=&quot;+item_id+&quot;&price=&quot;+price) %> &quot;>
<img src=&quot;images\addbasket.gif&quot;></a></td>
</tr>

//6. clean up all the objects
<% } RS.close(); connection.close(); %>

</table>


// store the basket items in a Javabean instance

<jsp:useBean id=&quot;basket&quot; class=&quot;ShoppingBasket&quot; scope=&quot;session&quot;/>

<% String title = request.getParameter(&quot;title&quot;);
if(title!= null)
{
String item_id = request.getParameter(&quot;item_id&quot;);
double price= Double.parseDouble(request.getParameter(&quot;price&quot;));
Product item = new Product(item_id, title, price);
basket.addProduct(item);
}

%>



</body>
</html>
 
Double check the available constructors for the Product object. I have not worked with it before but generally this error means your attempting to give more arguments to the constructor than it was expecting. If you can post more information on the Product class I may be able to help further.

-Tarwn ________________________________________________________________________________
Want to get great answers to your Tek-Tips questions? Have a look at faq333-2924
 
Thanks Tarwn,

I had a look at the class and still can't see it. I think I have a sheet over my eyes. Thanks again for you help.

The class. Product.java is below

public class Product{

/* define the variables */
String id, title;
int quantity;
double price;

/*define the class
public Product(String id, String title, double price){

this.quantity = 1;
this.id = id;
this.title= title;
this.price = price;
}

/*define accessor methods */
public String getID(){
return id;}

public double getPrice(){
return price;}

public int getQuantity(){
return quantity;}

public String getTitle(){
return title;}

public double getTotal(){
return price * (double) quantity;}



shop-product.jsp is below:

<%@ page language=&quot;java&quot; contentType=&quot;text/html&quot; import=&quot;ShoppingBasket
,Product, java.sql.*, java.util.*&quot; errorPage=&quot;errorpage.jsp&quot; %>

<html><head><title> Welcome to the Shop </title></head>

<body >

<table width=&quot;385&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; >

<tr> <td colspan=&quot;4&quot;>Roberts Record Store products:</td> </tr>
<tr> <td colspan=&quot;4&quot; align=&quot;right&quot;>

//hyperlink to view basket

<a href=&quot;<%= response.encodeURL(&quot;shop-basket.jsp&quot;) %>&quot;>
<img src=&quot;images\viewbasket.gif&quot;></a></td>

</tr>

<tr><td><b>Ref</b></td> <td><b>Title</b></td>
<td><b>Price</b></td> <td></td> </tr>

//get records from the items table in the shop database
<%
//1. Load the mySql jdbc driver

Class.forName(&quot;org.gjt.mm.mysql.Driver&quot;);

//2. open a connection to the &quot;shop&quot; database
java.sql.Connection connection = java.sql.DriverManager.getConnection
(&quot;jdbc:mysql://localhost/shop&quot;);

//3. create a statement object for sending SQL queries
java.sql.Statement stmt=connection.createStatement();

//4. place query results in a ResultSet object
java.sql.ResultSet RS = stmt.executeQuery(&quot;SELECT * FROM items&quot;);

//write a HTML table containing each record and assign ResultSet's column 1 to a string variable

int rowCounter = 0;
while (RS.next()) {

String item_id = RS.getString(&quot;item_id&quot;);
String title = RS.getString(&quot;title&quot;);
String description = RS.getString(&quot;description&quot;);
String price = RS.getString(&quot;price&quot;);
rowCounter++;

String bg = (rowCounter %2 !=0) ?&quot;#C0C0C0&quot;:&quot;#FFFFFF&quot;;


%>

<tr bgcolor=&quot;<%= bg %>&quot;>
<td><%= item_id %><td/>
<td><b><%= title %></b><br/>
<%= description %></td>
<td>£<%= price %></td>

<td>

//hyperlink to add an item to the basket


<a href=&quot;<%= response.encodeURL (&quot;shop-products.jsp?title=&quot;+title+&quot;&item_id=&quot;+item_id+&quot;&price=&quot;+price) %> &quot;>
<img src=&quot;images\addbasket.gif&quot;></a></td>
</tr>

//6. clean up all the objects
<% } RS.close(); connection.close(); %>

</table>


// store the basket items in a Javabean instance

<jsp:useBean id=&quot;basket&quot; class=&quot;ShoppingBasket&quot; scope=&quot;session&quot;/>

<% String title = request.getParameter(&quot;title&quot;);
if(title!= null)
{
String item_id = request.getParameter(&quot;item_id&quot;);
double price= Double.parseDouble(request.getParameter(&quot;price&quot;));
Product item = new Product(id, title, price);
basket.addProduct(item);
}

%>



</body>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top