Java Int Cannot Be Dereferenced Error
Contents |
Certification Databases Caching Books Engineering Languages Frameworks Products This Site Careers Other all forums Forum: Beginning Java int cannot be dereferenced error Collete Williams int cannot be dereferenced tostring Greenhorn Posts: 4 posted 5 years ago I know this error has
Int Cannot Be Dereferenced Meaning
been written numerous times, but I am still confused about why I received the error. I am very int cannot be dereferenced compareto 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
Int Cannot Be Dereferenced Length
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 int cannot be dereferenced equals java 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 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" 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 Learn more about hiring
Boolean Cannot Be Dereferenced Java
developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask
Int Cannot Be Converted To String
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 int compareto java them; it only takes a minute: Sign up Error of int cannot be dereferenced? up vote 0 down vote favorite I am getting an error with this constructor, and i have no idea how to fix? I am a https://coderanch.com/t/536655/java/java/int-dereferenced-error beginner at java. This is from an example exercise that i was trying to learn: /** * Create an array of size n and store a copy of the contents of the * input argument * @param intArray array of elements to copy */ public IntArray11(int[] intArray) { int i = 0; String [] Array = new String[intArray.length]; for(i=0; i * The representation is exactly the one returned b 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 http://stackoverflow.com/questions/15857377/new-to-java-and-have-the-error-int-cannot-be-dereferenced hiring developers or posting ads with us Stack Overflow Questions Jobs 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 https://www.daniweb.com/programming/software-development/threads/379119/error-error-int-cannot-be-dereferenced other. Join 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 cannot be a while, but keep receiving the error: int cannot be dereferenced. I saw couple of similar questions but still cannot figure out 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) int cannot be { 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.5k67192 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 not a class, so you can never use the . operator with an int variable. share|improve this answer answered Apr 7 '13 at 0:06 Code-Apprentice 27.6k353121 add a comment| Your Answer draft saved draft discarded Sign up or log in Sign up using Google Sign up using Facebook Sign up using Email and Password Post as a guest Name Email Post as a guest Name Email discard By posting your answer, you agree to the privacy policy and terms of service. Not the answer you're looking for? Browse other questions tagged java or ask your own question. asked 3 years ago viewed 9971 times active 3 years ago Blog Stack Overflo need some help in my programme. I have an error message: "error: int cannot be dereferenced" at line 134. Could you help me solve this problem, as I do not know what it means? Many thanks. /** * @(#)PrizeCollection.java * * PrizeCollection application * * @author * @version 1.00 2011/8/25 */ import javax.swing.JOptionPane; import java.lang.*; import java.util.*; public class PrizeCollection { public static void main (String[]args) { // Declare variables and arrays which will be used in the programme String[] Description = new String [3]; String[] Color = new String [3]; int[] Value = new int [3]; //Call the Menu optionMenu(Description,Color,Value); } //Method for the menu that will appear each time a task is completed public static void optionMenu(String Description[], String Color[], int Value[]) { int option = 0; while (option!=1 || option!=2 || option!=3 || option!=4){ option = Integer.parseInt(JOptionPane.showInputDialog("Please choose an option: \n" + "1 - Enter the details of a prize \n" + "2 - Print the details stored for all prizes \n" + "3 - Search for a prize with a particular value or by description \n" + "4 - Quit")); if (option==1) enterDetails(Description,Color,Value); if (option==2) printDetails(Description,Color,Value); if (option==3) searchDetails(Description,Color,Value); if (option==4) System.exit(0);} System.exit(0); } //Method to enter details for each prize public static void enterDetails(String Description[], String Color[], int Value[]){ for (int i=0; i<3; i++){ Description [i] = JOptionPane.showInputDialog (null, "Please enter the description of the prize: "); Color [i] = JOptionPane.showInputDialog (null, "Please enter the color of the prize: "); Value[i] = Integer.parseInt(JOptionPane.showInputDialog(null, "Please enter the value of the prize: ")); } optionMenu(Description,Color,Value); } // Method to search prizes public static void searchDetails(String Description[], String Color[], int Value[]) { // A Menu if needed to choose between search by description or by value int choice = 0; while (choice!=1 || choice!=2 || choice!=3){ choice = Integer.parseInt(JOptionPane.showInputDialog("Please choose an option: \n" + "1 - Search prize by description \n" + "2 - Search prize by value \n" + "3 - Return to the previous menu")); if (choice==1) searchDescription(Description,Color,Value); if (choice==2) searchValue(Description,Color,Value); if (choice==3) optionMenu(Description,Color,Value);} System.exit(0); } //Method to search by description public