aspose file tools*
The moose likes Java in General and the fly likes  int cannot be dereferenced  Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark " int cannot be dereferenced  " Watch " int cannot be dereferenced  " New topic
Author

int cannot be dereferenced

wong chang kit
Greenhorn

Joined: May 15, 2012
Posts: 20
It showed int cannot be dereferenced


Anyone knows how to solve it?

DAO dao = new DAO();

if(searchItem.equals("") && searchDes.equals("") && searchValue.equals("") ){
mr = dao.getjew(mr);}
else if(searchItem != ("")){
mr = dao.getSearching(mr, searchItem);}
else if(searchDes != ("")){
mr = dao.getDescription(mr, searchDes);}
else if(searchValue != ("")){
mr = dao.getDescription(mr, searchValue);}
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

wong chang kit wrote:It showed int cannot be dereferenced


Anyone knows how to solve it?


By not trying to dereference a primitive, such as an int.


Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Also, comparing Strings with == and != is wrong:

wong chang kit
Greenhorn

Joined: May 15, 2012
Posts: 20


if(searchItem.equals("") && searchDes.equals("") && searchValue.equals("")){
mr = dao.getjew(mr);}
else if(!searchItem.equals("")){
mr = dao.getSearching(mr, searchItem);}
else if(!searchDes.equals("")){
mr = dao.getDescription(mr, searchDes);}
else if(!searchDes.equals("")){
mr = dao.getVal(mr, searchValue);}


It shows error on if(searchItem.equals("") && searchDes.equals("") && searchValue.equals("")){

"Int cannot be dereferenced"

I'm so sorry that I dont really know how to solve it.

searchItem is String
searchDes is String
searchValue is Int

is it cannot combine them together?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

wong chang kit wrote:
searchValue is Int


So then why are you trying to do this?


First, as the error message is telling you, you can't do .equals() on a primitive such as int, because primitives don't have members.

Second, what sense does it make to compare an int to ""? That's like asking if the int is yellow or if it tastes good. What are you trying to do with searchValue.equals("")?
wong chang kit
Greenhorn

Joined: May 15, 2012
Posts: 20
I wanna search a Value(int) from "searchVal"

Then what should I do? I don't know how to separate it.
wong chang kit
Greenhorn

Joined: May 15, 2012
Posts: 20
Actually I just tried to modify the code that given from my lecturer. He declared a String and now im trying to declare an INT
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

wong chang kit wrote:Actually I just tried to modify the code that given from my lecturer. He declared a String and now im trying to declare an INT


Then you need to understand the difference between an object and a primitive. And you need to understand what someString.equals("") means, and you have to decide what you want to do instead with an int, since it doesn't make sense to ask if an int is equal to "" any more than it makes sense to ask if an int is equal to "hippopotamus."

If you want help, you'll have to explain what you're actually trying to accomplish. If you just start randomly changing things, you should expect that stuff will break.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11406
    
  16

wong chang kit wrote:
searchValue is Int

I doubt this is true. Java has a class called "Integer", and a primitive called "int". Since Java is case sensitive, "Int" doesn't mean anything...unless you have declared your own class by that name.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

fred rosenberger wrote:
wong chang kit wrote:
searchValue is Int

I doubt this is true. Java has a class called "Integer", and a primitive called "int". Since Java is case sensitive, "Int" doesn't mean anything...unless you have declared your own class by that name.


... in which case it wouldn't give "int cannot be dereferenced."

But yes, @wong chang kit, Fred makes a great point: You'll get better help here (and have better luck programming) if you pay closer attention to detail.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11406
    
  16

Jeff Verdegan wrote:... in which case it wouldn't give "int cannot be dereferenced."

well...the OP did say:
wong chang kit wrote: "Int cannot be dereferenced"
wong chang kit
Greenhorn

Joined: May 15, 2012
Posts: 20
if(searchItem.equals("") && searchDes.equals("")){
mr = dao.getjew(mr);}
else if(!searchItem.equals("")){
mr = dao.getSearching(mr, searchItem);}
else if(!searchDes.equals("")){
mr = dao.getDescription(mr, searchDes);}
else if(!searchDes.equals("")){
mr = dao.getVal(mr, searchValue);}

if(searchValue< (0))
mr = dao.getjew(mr);
else
mr = dao.getVal(mr, searchValue);





Actually I wanted to combine these two if else statements into one if else statement, because all the functions will not be able to work If I don't combine it into one if else statement
wong chang kit
Greenhorn

Joined: May 15, 2012
Posts: 20
By the way, I declared these 3 as Int and String

String searchItem = "";
String searchDes = "";
int searchVal = 0;
Sudhir Ravindra
Greenhorn

Joined: May 31, 2010
Posts: 16
wong chang kit wrote:By the way, I declared these 3 as Int and String

String searchItem = "";
String searchDes = "";
int searchVal = 0;


you cannot declare int searchVal and also do searchVal.equals. Use searchVal == 0 to check for the default value.
wong chang kit
Greenhorn

Joined: May 15, 2012
Posts: 20
but what I want to do is to search a value that less than the value that the user enters.
Sudhir Ravindra
Greenhorn

Joined: May 31, 2010
Posts: 16
wong chang kit wrote:but what I want to do is to search a value that less than the value that the user enters.


check if this is what you want to do, ensure that your DAO methods are structured appropriately.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11406
    
  16

wong chang kit wrote:By the way, I declared these 3 as Int and String

String searchItem = "";
String searchDes = "";
int searchVal = 0;

You are contradicting yourself. You say "I declared these 3 as Int...". do you see the "I" you typed there? then down below, you say "int searchVal". Do you see the lower-case "i"?

Java is case sensitive. that means that to Java, "Int" is NOT THE SAME THING as "int". People who write java know and understand this. So when you write contradictory things, we get confused. We don't know what you mean, we don't know what you REALLY did, and it looks like you don't care enough to put any effort in your posts. Many people will figure "well, if they don't care enough to make a decent, understandable post, I'm not going to bother trying to help."

I will give it one more shot, but if your response isn't better and more carefully thought out, showing that you have made some effort to understand and try what i'm saying, i'll probably walk away as well.

First and foremost...do you understand the difference between an Object and a primitive? Do you know there IS a difference? Or do those words not make any sense?

We have to establish what you know and understand before we go any further.
wong chang kit
Greenhorn

Joined: May 15, 2012
Posts: 20
I'm so sorry about that, because I'm new in Java. Please forgive me if I had done any mistakes.

By the way,



When I compiled it, the system just read searchItem and searchDes. It does not read searchValue. It displays "0"

Anyone knows what's the problem?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

wong chang kit wrote:I'm so sorry about that, because I'm new in Java. Please forgive me if I had done any mistakes.


Yes, we know that you're new. That's fine, every body has to start somewhere. That's not the issue. The issue is that you are not describing your problem clearly and not paying attention to what people are trying to tell you. That has nothing to do with Java or with being new to Java. That's just basic communication and problem solving.


By the way,



When I compiled it, the system just read searchItem and searchDes. It does not read searchValue. It displays "0"

Anyone knows what's the problem?


Explain what you think these lines do. How do YOU think they work? What do YOU think they are for?


At the moment, you seem to be programming by accident, without actually understanding the code that you are writing (or that you are receiving from others and then using).
wong chang kit
Greenhorn

Joined: May 15, 2012
Posts: 20
Hi, I already solved the problem. I should put 0, because if the textfield is equal to 0 then it will continue to the next step or display error message.

thanks for the guidance. My brain has been stuck in these few days.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Cool. Glad you got it sorted out!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: int cannot be dereferenced