Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: int cannot be dereferenced

  1. #1
    Member Wolverine89's Avatar
    Join Date
    Aug 2013
    Location
    Netherlands
    Posts
    101
    My Mood
    Happy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default int cannot be dereferenced

    hello,

    why do i get the error/message "int cannot be dereferenced" in bluej?

    import java.util.Random;
     
    public class KassaSimulator
    {
            private int nKlanten,  vKlanten,  tSteps,  klantnummer;
            private Queue[] kassas;
            private Random rng;
     
            public KassaSimulator(int kassas, int aantalKlanten, int verwerkKlanten, int tijdstappen)
            {
                this.kassas = new Queue[kassas];
     
     
     
                for (int i = 0; i < kassas.length; i++)
                {
                    kassas[i] = new Queue(aantalKlanten * 2); 
                }

    i get the error within the first for loop

    for (int i = 0; i < kassas.length; i++)
                {
                    kassas[i] = new Queue(aantalKlanten * 2); 
                }
    it is highlighting the kassas.length
    System.out.println(" dream in code ");


  2. #2
    Member
    Join Date
    Aug 2013
    Posts
    95
    Thanks
    3
    Thanked 14 Times in 14 Posts

    Default Re: int cannot be dereferenced

    it is referring to the integer being passed into the method, If you want to refer to the queue then you need to add the keyword "this"

    for (int i = 0; i < this.kassas.length; i++)
                {
                    kassas[i] = new Queue(aantalKlanten * 2); 
                }

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: int cannot be dereferenced

    The int variable kassas is a parameter of the method KassaSimulator() here:
    public KassaSimulator(int kassas, int aantalKlanten, int verwerkKlanten, int tijdstappen)
    //                               ^
    //                                | here kassas is a local int variable to the method
    and then kassas is used as an array of (? what) in the for loop:
    for (int i = 0; i < kassas.length; i++)
    //                               ^
    //                                | here kassas is being treated as an array with attribute 'length'
                {
                    kassas[i] = new Queue(aantalKlanten * 2); 
                }
    Since ints have no attributes and cannot have methods called on them like objects, then you're getting the error.

    Bottom line is, you can't do that with an 'int', and you shouldn't be trying to.

    Edit: Another good example of why keeping variable/method names meaningful and unique reduces confusion and errors.

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: int cannot be dereferenced

    This is a scope issue. You have your queue named: "kassas", but you also have a variable in the parameters of your constructor named: "kassas", which is an int. When you don't use a qualifying keyword (such as this.), java will look for the variable with the closes scope. In this case, it is attempting to call the .length method on your kassas int parameter, not your kassas queue.
    One solution is to use this.kassas.length (like you do when you construct the queue a few lines before), but a potentially "better" solution would be to rename the int kassas parameter to something which makes more sense (perhaps kassasLength).
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. Bug in Java ?? substring( int x, int y)
    By cheringalho in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 19th, 2013, 10:51 AM
  2. How to create an Int or double of what the user types int a JTextField?
    By Speedstack79 in forum Object Oriented Programming
    Replies: 2
    Last Post: January 13th, 2013, 11:00 PM
  3. List methods add(int k, Data data), set(int k, Data data), remove(int k)
    By Enirox in forum Object Oriented Programming
    Replies: 3
    Last Post: September 20th, 2012, 06:43 AM
  4. [SOLVED] Char cannot be dereferenced
    By zukipuu in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 17th, 2010, 11:13 PM
  5. Char cannot be dereferenced!! Please help
    By humdinger in forum Object Oriented Programming
    Replies: 5
    Last Post: February 14th, 2010, 03:18 PM