happy face
happy face

Reputation: 43

Int cannot be dereferenced. How do I fix this?

I am a beginner in programming. I know why I am getting this error at int result merge(list1)..., but I don't know how to fix this error. I did look at some of the questions related to this error here, but I am still confused. I do not want anyone to write a code for me, but I would appreciate some explanation. Thank you.

public int merge(int list1){
   try{
  int count = 0;
  for(int i= 0; i < 2; i++){
    count++;
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Please input the name of the file to be opened for " + count + " list: ");
    String filename = keyboard.nextLine();

    File file = new File(filename);
    Scanner inputFile = new Scanner(file);
    System.out.print("The list " + count + " is: " );

    while(inputFile.hasNext()){
      if(!inputFile.hasNextInt()){
        String ss = inputFile.next();
      }else{
        int length = 1;
        list1 = inputFile.nextInt();
        System.out.print(list1 + " ");

        } 

      } 

     System.out.println();  
  }

  int result = merge(list1).insertEnd(list1);


} catch (Exception e) {
  System.out.println("could not find the file");
}
  return result;  
}

Upvotes: 0

Views: 264

Answers (1)

Olivier Croisier
Olivier Croisier

Reputation: 6149

In the expression merge(list1).insertEnd(list1);, merge(list1) has type int because that's the return type of the merge method.

On this expression of type int, you're trying to call the insertEnd method, which cannot work because int is a primitive type and has no such method.

Your error message "cannot be dereferenced" explains that int is not a reference type (it is an int, which is a primitive type), so it cannot be "de-referenced" (meaning, you cannot follow its pointer to find an instance in memory on which you could call the insertEnd method)

Upvotes: 2

Related Questions