Error Int Cannot Be Dereferenced
Contents |
This Site Careers Other all forums Forum: Beginning Java int cannot be dereferenced error Collete Williams Greenhorn Posts: 4 posted 5 years ago I know this error has been written numerous times, but I am still confused about why I error int cannot be dereferenced java received the error. I am very new to Java, this is my first and
Int Cannot Be Dereferenced Java Code
last programming class. This is the first time I have seen this error and lost. I know it has something to do int cannot be dereferenced using java with the int and string. I am confused all together how it works. Code is suppose to accept an integer from user, go thru the array and find the element of that array and display
Variable Cannot Be Dereferenced
in a textfield. It also has to handle 2 specific exceptions. Can someone please tell me what it is suppose to look like. Thank you.. int cannot be dereferenced-line 53 //import java.lang.*; public class showElement implements ActionListener{ public void actionPerformed(ActionEvent e){ randomNumber = new int [100]; for (int x = 0; x < randomNumber.length; x++) randomNumber[x] = (int)(Math.random() * 1000); String inputString; inputString=valueField.getText(); try{ valueField.setText(randomNumber[Integer.parseInt(inputString)].toString());//error section } catch (IndexOutOfBoundsException ex){ valueField.setText("Out of what does int cannot be dereferenced mean bounds"); } catch (NumberFormatException ex){ valueField.setText("Not a Integer."); } } } Kurt Van Etten Ranch Hand Posts: 98 posted 5 years ago 1 Hi Collete, and welcome to the Ranch! The error you're getting, in line 17 of the code excerpt you posted, is because you're trying to call the toString() method on an int (since that's what's stored in the array), and int is a primitive type. There are several ways you could convert the int to a String: for example, you could concat it with an empty string to have it implicitly converted, or you could use the static Integer.toString() method to explicitly convert it . Collete Williams Greenhorn Posts: 4 posted 5 years ago I read up on on what it means to "concat it with an empty string" and "static Integer.toString() method" but I'm sorry I am still confused. Originally I had major help with that line but it looks like it did not work. Are you saying this line: valueField.setText(randomNumber[Integer.parseInt(inputString)].toString()); needs to changed to one of the options? Been trying for 2 weeks to get this working and I am burned out majorly and have gone brain dead. Is it possible for you to show me if not maybe a example. Campbell Ritchie Sheriff Posts: 50445
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the
Int Cannot Be Dereferenced Compareto
workings and policies of this site About Us Learn more about Stack
Int Cannot Be Dereferenced Length
Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs int cannot be dereferenced for loop Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join https://coderanch.com/t/536655/java/java/int-dereferenced-error them; it only takes a minute: Sign up New to Java and have the error “int cannot be dereferenced” up vote 5 down vote favorite I'm new to java and I've been working on this exercise for a while, but keep receiving the error: int cannot be dereferenced. I saw couple of similar questions but still cannot figure out http://stackoverflow.com/questions/15857377/new-to-java-and-have-the-error-int-cannot-be-dereferenced my own case. Here is the complete codes: package inclass; class OneInt { int n; OneInt(int n) { this.n = n; } @Override public boolean equals(Object that) { if (that instanceof OneInt) { OneInt thatInt = (OneInt) that; return n.equals(thatInt.n); // error happens here } else { return false; } } public static void main(String[] args) { Object c = new OneInt(9); Object c2 = new OneInt(9); System.out.println(c.equals(c2)); System.out.println(c.equals("doesn't work")); } } Thank you very much for helping me with this little trouble. java share|improve this question edited Apr 7 '13 at 0:08 paulsm4 57.3k67192 asked Apr 7 '13 at 0:04 Tian 3313 add a comment| 2 Answers 2 active oldest votes up vote 7 down vote accepted equals is a method of a class. int is a primitive, not a class. Simply use == instead: return n == thatInt.n; share|improve this answer answered Apr 7 '13 at 0:06 Dukeling 36.4k94080 add a comment| up vote 4 down vote To compare ints, just use the == operator: if (n == thatInt.n) Note that int is no
int cannot be dereferenced Error ? I'm following the java track and I was trying to https://teamtreehouse.com/community/gokartjava18-error-int-cannot-be-dereferenced-error increment a battery charge. and I don't have any idea what this error means Brendon Butler 3,592 Points Brendon Butler Brendon Butler 3,592 Points 10mo ago Could you post your code? 1 Answer PRO Rebecca Rich Pro Student 7,102 Points Rebecca Rich Rebecca Rich Pro Student 7,102 Points 10 months ago cannot be Usually this will happen if you try to call a method on an int (which is a primitive type). So for example, something like this: int count = 1; String counterText = "count" + count.toString(); The .toString() method call directly on the integer would throw that error. If that does not help cannot be dereferenced you find your problem or if you have additional questions / code snippets, let us know! Kele Heart 1,039 Points Kele Heart Kele Heart 1,039 Points 10mo ago Yes this was my problem thank you so much Posting to the forum is only allowed for members with active accounts. Please sign in or sign up to post. Our mission is to bring affordable, technology education to people everywhere, in order to help them achieve their dreams and change the world. Our Company About Us Contact Us Community Treehouse Stories Student Perks Treehouse Blog Affiliate Program Careers Topics HTML CSS Design JavaScript Ruby PHP WordPress iOS Android Development Tools Business Tracks Web Design Web Development Rails Development PHP Development iOS Development Android Development WordPress Development Starting a Business Treehouse Blog About Treehouse Privacy Policy Terms & Conditions © 2016 Treehouse Island, Inc. Sign in Email Address Password I forgot my password Sign in