Results 41 to 52 of 52
Thread: Method call
- 07-16-2011, 10:31 PM #41
Member
- Join Date
- Jul 2011
- Posts
- 98
- Rep Power
- 0
- 07-16-2011, 10:49 PM #42
Please explain. There are many ways for code to not work.it don't work
- 07-16-2011, 11:23 PM #43
Member
- Join Date
- Jul 2011
- Posts
- 98
- Rep Power
- 0
I'm trying to return quantity in the item in stock, but when I insert a number which I know not exist then I get a nullpointer
Java Code:if(findProduct(id).getID() != id) or (findProduct(id).getQuantity() == 0) Here I try to see if the id I insert and the product id is different or product id and its quantity is 0 If they are different or 0 return 0, if it is a match between product id and the inserted id then return quantity. But I have no idea of how to do it correct. Any suggestion?
- 07-16-2011, 11:31 PM #44
You must test for the null return BEFORE you try to use the returned value.I get a nullpointer
Get a Product reference from findProduct() and test it for null BEFORE you call the getID() method.
- 07-17-2011, 12:01 AM #45
Member
- Join Date
- Jul 2011
- Posts
- 98
- Rep Power
- 0
Could you please tell me if this look correct
When I use the print out, it print correct number. If write number 2 in the parameter, it print "not a valid number" since there don't exist anything with that number.
If i insert number 37, it print quantity 0.
So it seems to work, but when I try to use the return then I don't know which of the return is returned, since they both return 0
Java Code:public int numberInStock(int id) { if(findProduct(id) == null) { System.out.println("Not a valid id " + id); } else if(findProduct(id).getID() == id) { // System.out.println("Quantity " + findProduct(id).getQuantity()); return findProduct(id).getQuantity(); }else{ } return 0; }
- 07-17-2011, 12:09 AM #46
Member
- Join Date
- Jul 2011
- Posts
- 98
- Rep Power
- 0
I have tested the code in a debugger an I can see that the correct return is returned. So I think this method work well now.
Thank for your help again Norman!
- 07-17-2011, 12:10 AM #47
Does it compile and execute and give you the results that you want?
- 07-17-2011, 12:15 AM #48
Member
- Join Date
- Jul 2011
- Posts
- 98
- Rep Power
- 0
Yes, it compile and execute and give the result that I want
does it code look correct? It should be correct now
- 07-17-2011, 12:30 AM #49
Looks can be deceiving. What matters is: Does it work correctly with all possible test data.
- 07-17-2011, 08:36 PM #50
Member
- Join Date
- Jul 2011
- Posts
- 98
- Rep Power
- 0
Hello,
Could someone tell me how I can call to a method which is a void method in another class?
- 07-17-2011, 08:55 PM #51
Member
- Join Date
- Jul 2011
- Posts
- 98
- Rep Power
- 0
I have done it. Its works
- 07-17-2011, 09:59 PM #52
Member
- Join Date
- Jul 2011
- Posts
- 98
- Rep Power
- 0
I 'm about to write a code to print all details of product below an inserted value
What is most correct, write a new method to do it or write a method which call another print method that allready exist?
I can use this method below to print out product below the inserted value
Java Code:public void printProductValue(int value) { { Iterator it = stock.iterator(); while(it.hasNext()) { Product product = (Product) it.next(); if(product.getQuantity() < value) { System.out.println("Product: " + product); } } } }Last edited by ralf; 07-17-2011 at 10:11 PM.
Similar Threads
-
call a JSP method
By sauravsinha in forum JavaServer Pages (JSP) and JSTLReplies: 4Last Post: 09-26-2011, 04:19 PM -
How do I call this method
By africanhacker in forum New To JavaReplies: 1Last Post: 06-29-2011, 04:03 PM -
How to call this method?
By Rockefella in forum New To JavaReplies: 12Last Post: 01-20-2011, 04:32 PM -
how to call method?
By leapinlizard in forum New To JavaReplies: 9Last Post: 04-29-2009, 11:55 PM -
cannot call private method from static method
By jon80 in forum New To JavaReplies: 3Last Post: 05-07-2008, 08:37 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks