Cannot Be Dereferenced Error Java
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring java int cannot be dereferenced developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question java int cannot be dereferenced array x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join java int cannot be dereferenced compareto them; it only takes a minute: Sign up “int cannot be dereferenced” in Java up vote 9 down vote favorite 2 I'm fairly new to Java and I'm using BlueJ. I keep getting this "Int cannot be dereferenced" error when
Int Cannot Be Dereferenced Java Meaning
trying to compile and I'm not sure what the problem is. The error is specifically happening in my if statement at the bottom, where it says "equals" is an error and "int cannot be dereferenced." Hope to get some assistance as I have no idea what to do. Thank you in advance! public class Catalog { private Item[] list; private int size; // Construct an empty catalog with the specified capacity. public Catalog(int max) { list = new Item[max]; java char cannot be dereferenced size = 0; } // Insert a new item into the catalog. // Throw a CatalogFull exception if the catalog is full. public void insert(Item obj) throws CatalogFull { if (list.length == size) { throw new CatalogFull(); } list[size] = obj; ++size; } // Search the catalog for the item whose item number // is the parameter id. Return the matching object // if the search succeeds. Throw an ItemNotFound // exception if the search fails. public Item find(int id) throws ItemNotFound { for (int pos = 0; pos < size; ++pos){ if (id.equals(list[pos].getItemNumber())){ //Getting error on "equals" return list[pos]; } else { throw new ItemNotFound(); } } } } java int bluej share|improve this question edited Jul 19 at 0:13 Sotirios Delimanolis 154k25245361 asked Oct 1 '13 at 6:08 BBladem83 183129 2 You're trying to using a int where an Integer, Number or Object is expected...int does not have any methods –MadProgrammer Oct 1 '13 at 6:09 add a comment| 5 Answers 5 active oldest votes up vote 9 down vote accepted id is of primitive type int and not an Object. You cannot call methods on a primitive as you are doing here : id.equals Try replacing this: if (id.equals(list[pos].getItemNumber())){ //Getting error on "equals" with if (id == list[pos].getItemNumber()){ //Getting error on "equals" share|improve this answer edited Mar 23 at 3:23 answered Oct 1 '13 at 6:10 Juned Ahsan 44.6k64774 add a
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business java char cannot be dereferenced charat Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation
Int Cannot Be Dereferenced Tostring
Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like
Int Cannot Be Dereferenced Length
you, helping each other. Join them; it only takes a minute: Sign up CharAt().equals() causes “char cannot be dereferenced” up vote 1 down vote favorite I am trying to check a string for hyphens at different positions http://stackoverflow.com/questions/19109131/int-cannot-be-dereferenced-in-java (for a phone number because the input varies), but I keep getting the error char cannot be dereferenced Code: do { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter String"); String raw = br.readLine(); if (raw.length() < 10) { System.out.println(""); System.out.println("Please input a valid phone number of at least 10 digits/letters"); System.out.println(""); } else { if (raw.charAt(3).equals('-') && raw.charAt(7).equals('-')) { System.out.println("2 Hyphens at 3 and 7"); } else if (raw.charAt(3).equals('-') && raw.charAt(8).equals('-')) { System.out.println("2 Hyphens at http://stackoverflow.com/questions/21068134/charat-equals-causes-char-cannot-be-dereferenced 3 and 8"); } else if (raw.charAt(3).equals('-') && raw.charAt(9).equals('-')) { System.out.println("2 Hyphens at 3 and 9"); } } } while (1 < 2); java share|improve this question edited Jan 11 '14 at 21:37 Jeroen Vannevel 29.7k1154103 asked Jan 11 '14 at 21:31 Ds.109 1693925 7 What does String#charAt() method returns? –Rohit Jain Jan 11 '14 at 21:32 char is a primitive type. It has no methods. But you can compare chars with == –milleniumbug Jan 11 '14 at 21:32 Side comment: that's not how you check for a hyphen in a string. I suggest you check String class methods. @milleniumbug Learn from Rohit Jain how to answer without answering ;) –m0skit0 Jan 11 '14 at 21:34 Why are you not using regex? –Bhesh Gurung Jan 11 '14 at 21:39 add a comment| 1 Answer 1 active oldest votes up vote 3 down vote accepted If you use something like this, it will work: if (raw.charAt(3) == '-' && raw.charAt(7) == '-') { System.out.println("2 Hyphens at 3 and 7"); } else if (raw.charAt(3) == '-' && raw.charAt(8) == '-') { System.out.println("2 Hyphens at 3 and 8"); } else if (raw.charAt(3) == '-' && raw.charAt(9) == '-') { System.out.println("2 Hyphens at 3 and 9"); } The problem is that raw.charAt(n) returns a char and not a String. The eq
New Topic programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Languages Frameworks Products This Site Careers Other all forums Forum: Beginning Java int cannot be dereferenced https://coderanch.com/t/536655/java/java/int-dereferenced-error 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 received the error. I am very new to Java, this is my first and last programming class. This is the first time I have seen this error and lost. I know it has something to do cannot be 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 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 cannot be dereferenced //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 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 wit