469,323 Members | 1,560 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,323 developers. It's quick & easy.

int cannot be dereferenced error message

6
Expand|Select|Wrap|Line Numbers
  1. package bankobject;
  2. import java.io.*;
  3. import javax.swing.JOptionPane;
  4.  
  5. class Number {
  6.    String studentName;
  7.    String teacherName;
  8.    int[] Number;
  9.  
  10. public Number () {
  11.    studentName = JOptionPane.showInputDialog("Please Enter student name");
  12.     teacherName = JOptionPane.showInputDialog("Please Enter the teacher name ");
  13.     int numNumber = Integer.parseInt(JOptionPane.showInputDialog("Please enter the number of numbers"));
  14.     Number[] obj = new Number [numNumber];
  15.     for (i = 0; i < numNumber.length; i++)
  16.        obj [i] = new Number ();   
  17. }
  18.  
I want the array to take as many numbers object, by using a for loop and instantiate each number object by calling the Number constructor.
Feb 15 '08 #1
11 11311
BigDaddyLH
1,216 Expert 1GB
I think you meant to write this:

Expand|Select|Wrap|Line Numbers
  1. for (int i = 0; i < numNumber; i++)
Feb 15 '08 #2
BigDaddyLH
1,216 Expert 1GB
There are also several logical errors in your code that you need to attend to. In your Number class you create an array full of Number objects -- that could lead to an infinite loop if you keep creating non-empty arrays. I think you meant the user interaction code to be elsewhere, perhaps in the main method, which hasn't shown up, yet. Also you have an int array called Number -- surely pure confusion on your part?
Feb 15 '08 #3
JPane
6
There are also several logical errors in your code that you need to attend to. In your Number class you create an array full of Number objects -- that could lead to an infinite loop if you keep creating non-empty arrays. I think you meant the user interaction code to be elsewhere, perhaps in the main method, which hasn't shown up, yet. Also you have an int array called Number -- surely pure confusion on your part?
Yeah, I meant for (int i = 0; i < numNumber; i++); forget the int there.

in the attributes list I wanted to make an array of number objects then in the constructor I would prompt the user for the number of number objects and use a for loop to instantiate each number account using the number constructor for each.

in the main method I would just instantiate two number objects.

The problem I'm having is in the for loop and getting the error " int cannot be dereferenced error message" I know I have to do something with it because it's a primitive, just don't know how in an array and for loop.
Feb 15 '08 #4
Ganon11
3,652 Expert 2GB
Don't know if this will fix your problem, but it looks like you declare a private data member of the Number class called int[] Number - that;s bound to give you problems. Consider a better name for it.
Feb 15 '08 #5
JPane
6
Don't know if this will fix your problem, but it looks like you declare a private data member of the Number class called int[] Number - that;s bound to give you problems. Consider a better name for it.

maybe int [] grade is better.


Still doesn't work since the error is coming with this line

for (int i = 0; i < numNumber; i++)
Feb 15 '08 #6
BigDaddyLH
1,216 Expert 1GB
I don't get syntax errors from your code when I change it as I indicated.
Feb 15 '08 #7
JPane
6
Thanks for the help.
Feb 15 '08 #8
JPane
6
There are also several logical errors in your code that you need to attend to. In your Number class you create an array full of Number objects -- that could lead to an infinite loop if you keep creating non-empty arrays. I think you meant the user interaction code to be elsewhere, perhaps in the main method, which hasn't shown up, yet. Also you have an int array called Number -- surely pure confusion on your part?
If I have two classes which are number and student how would I call them up in the main method when I just instantiate a number object. Meaning the input attributes in the student class.

Both classes are composition of each other.
Feb 15 '08 #9
BigDaddyLH
1,216 Expert 1GB
If I have two classes which are number and student how would I call them up in the main method when I just instantiate a number object. Meaning the input attributes in the student class.

Both classes are composition of each other.
I don't understand what you are trying to say. perhaps is you posted some code to explain what you are trying to do.
Feb 15 '08 #10
JPane
6
Expand|Select|Wrap|Line Numbers
  1.  
  2.       package studentobject;
  3.  
  4.       import java.io.*;
  5.  
  6.       import javax.swing.JOptionPane;
  7.  
  8.  
  9.       class Number {
  10.  
  11.          String studentName;
  12.  
  13.  
  14.          int[] Number;
  15.  
  16.  
  17.       public Number () {
  18.  
  19.          studentName = JOptionPane.showInputDialog("Please Enter student name");
  20.  
  21.           teacherName = JOptionPane.showInputDialog("Please Enter the teacher name ");
  22.  
  23.           int numNumber = Integer.parseInt(JOptionPane.showInputDialog("Please enter the number of numbers"));
  24.  
  25.           Number[] obj = new Number [numNumber];
  26.  
  27.           for (i = 0; i < numNumber.length; i++)
  28.  
  29.              obj [i] = new Number ();   
  30.  
  31.       }
  32.  
  33.  
  34. class Student
  35.  
  36. {
  37.  
  38.     String schoolName;
  39.  
  40.  
  41. // Methods
  42.  
  43.  
  44.     void  students()
  45.  
  46.     { 
  47.  
  48.  
  49.         double students = 0;
  50.  
  51.  
  52.     }
  53.  
  54.  
  55.  
  56.     // Constructor
  57.      public Student ()
  58.  
  59.  {
  60.  
  61.  
  62.    schoolName = JOptionPane.showInputDialog("Please Enter the school name ");
  63.  
  64.  
  65.  }
  66.  
  67.   }
  68.  
  69.  
  70. }
  71. public class StudentOject  {
  72.  
  73.  
  74.     public static void main(String[] args) {
  75.  
  76.       Number nu = new Number ();
  77.  
  78.  
  79.    String output = "Student Name " + nu.studentName + "      Teacher Name: "  + nu.teacherName
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.         JOptionPane.showMessageDialog(null, output, "Student Information",
  90.                 JOptionPane.INFORMATION_MESSAGE);
  91.  
  92.  
  93.     }
  94.  
  95.  
  96. }   
  97.  
  98.  
  99.  
  100.  
  101.  
Since the student class is part of the number class when I do the output it isn't going to work with the student class because I only made a new object in the number class.
Feb 16 '08 #11
BigDaddyLH
1,216 Expert 1GB
Expand|Select|Wrap|Line Numbers
  1. public Number () {
  2.     ...
  3.     int numNumber = Integer.parseInt(JOptionPane.showInputDialog("Please enter the number of numbers"));
  4.     Number[] obj = new Number [numNumber];
  5.     for (i = 0; i < numNumber.length; i++)
  6.         obj [i] = new Number ();   
  7. }
  8.  
I can't get past this constructor. Do you understand it? When you construct a Number object, you prompt the user for a size, allocate an array and then construct more Number objects, each of which will prompt the user for a size, allocate an array and then construct more Number objects, each of which will prompt the user for a size, allocate an array ...

I think you need to start over again. I still have no idea what you are trying to do, and I'm not convinced anyone does, including yourself. First, answer these questions.
  1. What is a Number object? What are its key responsibilities?
  2. What is a Student object? What are its key responsibilities?
  3. What is a StudentObject object? What are its key responsibilities?
Feb 16 '08 #12

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by Christopher Brandsdal | last post: by
1 post views Thread by Bob | last post: by
4 posts views Thread by Chris | last post: by
2 posts views Thread by Christophe | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by mdpf | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by listenups61195 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.