Results 1 to 3 of 3
  1. #1
    mlnsas is offline Member
    Join Date
    Oct 2012
    Posts
    1
    Rep Power
    0

    Exclamation char cannot be dereferenced! help guys! java is just too crazy

    HI sorry to disturb you guys but i really need help with my coding. I am pretty new to java and had some problems with it.

    The original code is :

    Java Code:
    public class StudentList
    {
    private List<Student> students;
    
    public Student findById(String id)
    {
    for(int i=0;i<students.size();i++)
    {
    Student s = students.get(i);
    if(s.getId().equals(id))
    {return s;}
    }
    }
    }
    the codes above work fine but
    When i want to add another search function to find from a collection, for another variable which is of a char type, it says CHAR CANNOT BE DEREFERENCED.

    here's the code:

    Java Code:
    public Student findByStatus(char status)
    {
    for(char i=0;i<students.size();i++)
    {Student s = students.get(i);
    if(s.getStatus().equals(status))
    {return s;}}
    }
    the "if(s.getStatus().equals(status))" part incurred "CHAR CANNOT BE DEREFERENCED"
    sorry to trouble you and thankyou
    Last edited by mlnsas; 10-19-2012 at 12:04 PM.

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

    Default Re: char cannot be dereferenced! help guys! java is just too crazy

    Please use [code] tags [/code] when posting code.
    Many people won't bother reading unformatted code.

    WHen you have an error you need to post the full error message and point out the line on which the error occurs.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: char cannot be dereferenced! help guys! java is just too crazy

    Quote Originally Posted by mlnsas View Post
    Java Code:
    public Student findByStatus(char status)
    {
      for(char i=0;i<students.size();i++)
      { 
        Student s = students.get(i);
        if(s.getStatus().equals(status))
        { 
          return s; 
        }
      }
    }
    the "if(s.getStatus().equals(status))" part incurred "CHAR CANNOT BE DEREFERENCED"
    sorry to trouble you and thankyou
    So it appears that the Student method getStatus() returns a char, correct? You can't call a method on a primitive such as a char, int, byte, double, etc... Instead why not simply use the equality operator:

    Java Code:
    if (s.getStatus() == status) {
      // ....
    }
    Another suggestion: please read up on Java coding conventions as your currently posted code is quite compressed with limited use of white-space, does not use indentation properly making very difficult for us to read. This is only important if you need to have others understand your code quickly and easily (such as when asking for free help here, or when your instructor grades your code). Note the difference between your code and the edits I've made to your code above to see how much easier the latter is to read.

    Much luck!

Similar Threads

  1. double cannot be dereferenced
    By Garcia65 in forum New To Java
    Replies: 3
    Last Post: 10-17-2012, 10:05 AM
  2. Simple java conversion driving me crazy!!
    By New2J@v@ in forum New To Java
    Replies: 7
    Last Post: 06-14-2012, 03:27 AM
  3. Crazy threads problem with Java on WINE
    By kjkrum in forum Advanced Java
    Replies: 7
    Last Post: 08-23-2011, 11:53 PM
  4. ERROR - char cannot be dereferenced HELP
    By jonytek in forum New To Java
    Replies: 6
    Last Post: 05-04-2011, 06:54 AM
  5. int cannot be dereferenced
    By jackrush in forum Advanced Java
    Replies: 2
    Last Post: 11-12-2010, 08:47 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •