-12

In the code below I am getting the error in the 2nd last line of Test_1 "System.out.println(set1.get(0).get(0).txt);" in "txt" please help. java error screenshot

import java.util.ArrayList;

public class Test_1 {


/** 
 * @param args the command line arguments 
 */ 
public static void main(String[] args) { 
     ArrayList<ArrayList> set1 = new ArrayList<>(); 
    ArrayList<abc> set2 = new ArrayList<>(); 
    ArrayList<abc> set3 = new ArrayList<>(); 

    set1.add(set2); 
    set1.add(set3); 
    set2.add(new abc("xxxxxx")); 
    set2.add(new abc("xxxxx yyyyy")); 
    System.out.println(set2.get(0).txt); 
    System.out.println(set1.get(0).get(0).txt);
    System.out.println(((abc) set1.get(0).get(0)).txt); 
} 

} 


class abc{
 String txt;  
 public abc(String txt)
 { 
    this.txt = txt; 
 }  
 }
Anoop
  • 167
  • 2
  • 10
  • 4
    Please [edit] your question make sure all code is properly formatted. Also, include the error **as text**, not in a linked image, and mark the line in the code where the error appears. – RealSkeptic Aug 14 '17 at 13:23
  • You need to call the method name, not the variable name. – khriskooper Aug 14 '17 at 13:23
  • 1
    `.txt` is not a field of `Object`. The poorly named variable `set1` should be of type `ArrayList>`. Never use raw types. – bcsb1001 Aug 14 '17 at 13:24
  • 1
    You solve the problem already on *the very next line of code*. Your non-generic `ArrayList` is not by default of type `abc`, but you can cast the elements to that type. – David Aug 14 '17 at 13:24
  • The real point here is: this is *basic* syntax. Don't try to do trial and error. Read a good book or tutorial, and follow that - instead of "inventing" your own syntax. – GhostCat Aug 14 '17 at 13:25
  • Possible duplicate of [What is a raw type and why shouldn't we use it?](https://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it) – Tom Aug 14 '17 at 13:42
  • thanks, @bcsb1001, solves the problem, and I got the problem also thanks. – Anoop Aug 14 '17 at 13:44

2 Answers2

-1

Because you are not giving a type for the Generic inside the first set1, so when you get from set1 you are not garunteed that the list you are getting back is a list of abc, and it is confusing you syntax checker.

Consider

 ArrayList<ArrayList> set1 = new ArrayList<>();

Vs.

 ArrayList<ArrayList<abc>> set1 = new ArrayList<>();
gbtimmon
  • 4,238
  • 1
  • 21
  • 36
  • No. It will definitely _not_ compile fine. It isn't "confusing a syntax checker," the return value of a raw `get` method is just `Object`. An `Object` has no field called `txt`. – Salem Aug 14 '17 at 13:27
-1

Look at set1 and compare it to set2 notice that in the second case you specifying exactly what kind of objects set2 will hold but in the set1 you don't so the compiler has no idea that you using your abc class (By the way class name should start with a capital). Here is the solution

  public static void main(String[] args) {
    ArrayList<ArrayList<abc>> set1 = new ArrayList<>(); // notice this line
    ArrayList<abc> set2 = new ArrayList<>();
    ArrayList<abc> set3 = new ArrayList<>();

    set1.add(set2);
    set1.add(set3);
    set2.add(new abc("xxxxxx"));
    set2.add(new abc("xxxxx yyyyy"));
    System.out.println(set2.get(0).txt);
    System.out.println(set1.get(0).get(0).txt);
    System.out.println(((abc) set1.get(0).get(0)).txt);
}
urag
  • 1,228
  • 9
  • 28