Results 1 to 6 of 6
  1. #1
    raider00321 is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default Array Required, but java.lang.String found

    Hey guys. First time visitor so hey once again ^^.
    Im trying to create a script that will let me store specific data in a set of objects using an arraylist, but have seemed to have hit a wall. with the error

    Array Required, but java.lang.String found

    Java Code:
    ArrayList<String> gates = new ArrayList<String>(200);
    
    public void loadGates() {
    	Connection con = null;
    	String gateName = "";
    	int xLoc;
    	int yLoc;
    	int zLoc;
    	String gates ="";
    	con = etc.getSQLConnection();
    	try
    	{
    		count = 0;
    		Statement st = con.createStatement();
    		ResultSet counter =st.executeQuery("SELECT COUNT(*****) FROM gates");
    		ResultSet res = st.executeQuery("SELECT * FROM *****");
    		while(res.next())
    		{
    			gateName =res.getString("gateName");
    			xLoc = res.getInt("*****");
    			yLoc = res.getInt("*****");
    			zLoc = res.getInt("*****");
    			gates[count]= new Gate(gateName,xLoc,yLoc,zLoc);
    			count++;
    		}
    
    	}
    	catch(SQLException SQLException1)
    	{
            log.info("SQL Error occured"+SQLException1);
    
    	}
    }
    Also, if it is also needed...
    Java Code:
    public class Gate
    {
    	String GName;
    	public int x;
    	public int y;
    	public int z;
    
        public Gate()
        {
    
        }
    
    	public Gate(String gateName, int xLoc, int yLoc, int zLoc)
    	{
    		this.GName = gateName;
    		this.x = xLoc;
    		this.y = yLoc;
    		this.z = zLoc;
    
    	}
    
    }
    This had really got me stumped.Could someone please explain whats goingon here and how to fix it?
    Much thanks,
    Raider00321

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,433
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by raider00321 View Post
    Array Required, but java.lang.String found

    Java Code:
    	String gates ="";
    			gates[count]= new Gate(gateName,xLoc,yLoc,zLoc);
    I can understand why the compiler is complaining: you want to treat a String as if it were an array.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    raider00321 is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default

    I thought that at first too, but that's not the case because that variable is an String, but is inside and object, which is a part of an array. So it should technically be ok. I still tried your suggestion however, but it did not fix the problem sadly.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,433
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by raider00321 View Post
    I thought that at first too, but that's not the case because that variable is an String, but is inside and object, which is a part of an array. So it should technically be ok. I still tried your suggestion however, but it did not fix the problem sadly.
    But you said so yourself in your code: "String gates", that makes "gates" of type String; all the rest is simply incorrect. Your local variable "gates" hides your class member variable "gates" (in general that's A Bad Thing (tm)). Use "this.gates" in that method if you want to deal with the member array. Besides, that member variable "gates" is an ArrayList and you can't access its elements with the angular bracket notation.

    kind regards,

    Jos
    Last edited by JosAH; 11-28-2010 at 01:57 PM.
    cenosillicaphobia: the fear for an empty beer glass

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

    Default

    Quote Originally Posted by raider00321 View Post
    I thought that at first too, but that's not the case because that variable is an String, but is inside and object, which is a part of an array. So it should technically be ok.
    As Jos has said, you've declared a variable as a String and are trying to use it as an array. This will never be OK. Period.

    I still tried your suggestion however, but it did not fix the problem sadly.
    Then you've implemented the suggestion wrong. You may wish to show this attempt so we can help you further.

  6. #6
    raider00321 is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default

    Sorry guys, its late... guess i didnt know what i was thinking(its too late to think)

    But yeah, that was the problem. I did however have a similar error with the arrayList, but i took a shortcut by turning it into a normal array which seemed to work.

    Thanks for the help guys!

Similar Threads

  1. Replies: 5
    Last Post: 03-17-2011, 02:26 PM
  2. Replies: 0
    Last Post: 07-06-2009, 02:38 PM
  3. Replies: 3
    Last Post: 05-21-2009, 03:48 PM
  4. Replies: 6
    Last Post: 10-08-2008, 08:09 AM
  5. Replies: 2
    Last Post: 09-18-2008, 07:30 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
  •