0

I know that it might be a basic question but the codes that i got for this question does not work on my arraylist. May be someone could help me solve this.I would like to access the arraylist "Connection" of class Testing1.Java and use it in another class named SQL.Java. My array "Connection" is an object of another array "NamedShape".

Here are my 2 arrayslist:

ArrayList<NamedShape> shapes = new ArrayList<NamedShape>();
        ArrayList<Connection> con = new ArrayList<Connection>();

        public class Connection {

            private NamedShape namedShape1;
            private NamedShape namedShape2;

            public Connection(NamedShape namedShape1, NamedShape namedShape2) {
                this.namedShape1 = namedShape1;
                this.namedShape2 = namedShape2;
            }

            public NamedShape getNamedShape1() {
                return namedShape1;
            }

            public NamedShape getNamedShape2() {
                return namedShape2;
            }

            public void setNamedShape1() {
                this.namedShape1 = namedShape1;
            }

            public void setNamedShape2() {
                this.namedShape2 = namedShape2;
            }
        }

        public class NamedShape {

            private String name;
            private Shape shape;

            public NamedShape(String name, Shape shape) {
                this.name = name;
                this.shape = shape;
            }

            public String getName() {
                return name;
            }

            public Shape getShape() {
                return shape;
            }
        }

I've put a getConnection Method inside my Testing1.Java. Data are inserted correctly inside the Arraylist Connection(I've not put the codes to add the data but data are inserted correctly, that's not the problem)

public ArrayList<Connection> getConnection() {
            return con;

        }

Here is my Trial SQL.Java but it does not recognize the getConnection() method here:

import java.util.ArrayList;

public class SQL {
    private Testing1 sql;
    public SQL(){
        sql = new Testing1();
        ArrayList<Connection> con = sql.getConnection()

    }

   public static void main(String args[]){
       new Test();


   }
}

Can someone please help me out with this?

Edited I've read and followed what you said and now i have this, but i dont know how the main should be. So i have called my class Testing1.java to ERDBUILDER.java. My ERDBUILDER class allows me to draw shapes and after some processing the shape is stored to an arraylist Connection. Then i have my class SQL.java where i want to use that arraylist Connection by calling the class SQL.java from the ERDBUILDER.java but i dont want java to open another ERDBUILDER.java. I've put new SQL(); in the main but it's opening another ERDBUILDER.java and that's not want i want. can you please suggest something? I might be a basic question but i still can't find a way.

package project;
import java.awt.Shape;
import java.util.ArrayList;
import project.ERDBUILDER.DrawingBoard.Attribute;
import project.ERDBUILDER.DrawingBoard.Connection;
import project.ERDBUILDER.DrawingBoard.NamedShape;


public class SQL {
    
    private ERDBUILDER sql;
    public SQL(){
        sql = new ERDBUILDER();
    ArrayList<Connection> con = sql.getDrawingBoard().getConnection();
            
        for (int a = 0; a < con.size(); a++) {
                                    NamedShape f = con.get(a).getNamedShape1();
                                    Attribute g = con.get(a).getNamedShape2();
                                    String i = f.getName();
                                    String j = g.getName();

                                    Shape y = f.getShape();
                                    Shape y1 = g.getShape();

                                   
        }
    }
    
   public static void main(String args[]){

       
       
   }
}

14
  • Please include the name of the files these code snippets are from, like SQL.java and such. Commented Mar 12, 2015 at 11:37
  • Can you post the source for your Testing1 class Commented Mar 12, 2015 at 11:38
  • where is Testing1 class Commented Mar 12, 2015 at 11:39
  • Incidentally, youre referring to both 'Connection' and 'NamedShape' as arrays. Neither of these is an array. You do have two fields called shapes and con, but these are ArrayLists of the relevant objects, rather than arrays. To avoid confusion (both for yourself and others) it may be worth correcting this. Commented Mar 12, 2015 at 11:49
  • @richzilla It's telling me that i've exceeded the number of character. That's why i did not post the Testing.Java. My Arrays are found inside my Testing1.Java and data are added to the array "Connection" So i just want to access the Array "Connection" from Testing1.Java and use it into Sql.Java Commented Mar 12, 2015 at 11:51

1 Answer 1

1

Although your code as you have posted on pastebin will compile, it is a very unusual pattern.

You have multiple nested inner classes within your Testing1 class. This is valid Java (i.e. it will compile), but probably isn't necessary for what you want.

Quick answer

getConnection() isn't available to you in the line

ArrayList<Connection> con = sql.getConnection()

because your getConnection() method is not defined in the class Testing1, but rather the inner class of an inner class Testing1.DrawingBoard.Connection (line 342 of your code).

Fix

  1. Make drawPanel a member variable of Testing1 so that you can access it from "outside", either directly by making it a public member, or by adding a getDrawingBoard() method that returns it. e.g.

    public class Testing1 extends JFrame
    {
        private DrawingBoard drawPanel;
    
        public DrawingBoard getDrawingBoard()
        {
            return drawPanel;
        }
    
        //...The rest of your existing code except !!!
        // !!! in the Testing1 constructor at line 238 !!!
        // change          final DrawingBoard drawPanel = new DrawingBoard(); to
    
            drawPanel = new DrawingBoard();
    
        // ... and the rest of your code...
    
    }
    
  2. Move getConnection() out of Connection - so it is a member of DrawingBoard rather than Connection. e.g. (using your existing code as base)

    //...preceding code before line 324 ...
    
    public class DrawingBoard extends JComponent
    {
    
        private static final long serialVersionUID = -4431176095451940075L;
    
        // ArrayList<Shape> shapes = new ArrayList<Shape>();
        ArrayList<Color> shapeStroke = new ArrayList<Color>();
        ArrayList<Integer> count = new ArrayList<Integer>();
        ArrayList<NamedShape> shapes = new ArrayList<NamedShape>();
        ArrayList<Connection> con = new ArrayList<Connection>();
    
        public ArrayList<Connection> getConnection()
        {
           return con;
        }
    
        public class Connection
        {
        // ... rest of your code...
    
  3. Then, you will be able to do the following in the constructor of SQL:

    public SQL(){
        sql = new Testing1();
        ArrayList<Connection> con = sql.getDrawingBoard().getConnection();
    }
    

Comment

The pattern you're using is quite hard to follow and maintain. More information on when to use nested classes is available in the Java tutorials.

In particular - you really don't need DrawingBoard to be an inner class of Testing1 - DrawingBoard could be used in many different applications, so it doesn't fulfill the main criterion

nested classes enable you to logically group classes that are only used in one place

I recommend a refactoring - break out DrawingBoard into a new class file DrawingBoard.java. Put it in the same package as Testing1 then it can be used by Testing1.

I'm aware this answer might throw up more questions - if it does - ask a new question about nesting classes versus importing them in your specific case and link to it in the comments...

Sign up to request clarification or add additional context in comments.

6 Comments

@J Richard Snape Thank you for your long and comprehensive response. But i dont want java to open another Testing1 class when i run the .SQL class. Can you please tell me what i should write in the main class? Please see my above edited code.
Hmm, I can see your edit. There are a couple of problems here: 1. Your question is "migrating"... it's now a completely different question to the original, so you should really take your edit and ask a new question, referencing this one. 2. I feel like there's a deeper misunderstanding here - possible an XY problem. I can't see why the code you have is in the Constructor of the SQL class. You're hinting that you might be wanting to acess the same ERDBUILDER from a number of different classes, but it's not clear...
In other words - ask a new question, with a bit more context, referencing this one. Explain exactly what you're trying to do - what is the SQL class actually for? Why not just invoke an ERDBUILDER? Is there a bigger problem / code context that you're trying to fit this into?
@J Richard Snape Ok i will post another new question based on that. In fact what i wanted to do is to create my SQL Statement in the sql.java but by using the arraylist from the ERDBUILDER.java class.
By the way - your latest edit to your question means it doesn't make much sense - no one can see the original classes etc now, so my answer would not be understandable to anyone else reading it. I suggest you roll back to the previous version - particularly as you've now asked this related question to cover your new issue.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.