-2

I'm trying to use object oriented design and it's a bit confusing. Let's say I have 3 separate objects

 public class Tool{
     String name;
     Float weight;
 }

 public class Drawer{
     String name;
     Tool[] tool;
 }

 public class Toolbox{
     String name;
     Drawer[] drawer;

 public void setDrawers(String[] drawers) {

    for(int i = 0; i < drawers.length; i++) {
        drawer[i].name = drawers[i];

    }
  }
}

So a Tool has a String and Float. Drawer has a String and an array of Tools. Toolbox has a String and array of Drawers.

So let's say in my main code I say...

 Toolbox myBox = new Toolbox();
 myBox.setDrawers(drawers);

I'm getting null pointer issues. What's the proper way to instantiate everything if I want to instantiate a toolbox? Each class is in a separate Java file is this ok, or should they be in one file? Since all classes are in the same app do I need to use the import statement? What would my Constructors look like? Is there anything else I'm missing? Thanks for the help.

Please do not mark this as a duplicate of the "null pointer" question. That does not address objects that contain arrays of other objects and how to instantiate everything.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Matt
  • 51
  • 5
  • 3
    Sorry, this basically too broad. You are asking like 10 different questions, all boiling down to: not enough studying yet. Seriously: Java itself requires a certain amount of learning time. Don't overburden yourself with Android and apps if you didn't study such basic stuff yet. – GhostCat Jun 09 '18 at 15:19
  • You have to make some efforts, here is how to avoid NullPointerException https://ideone.com/BJrqZq hope this can gives you a push to start – Youcef LAIDANI Jun 09 '18 at 15:25
  • Ghostcat, I've read an entire book on programming Java on Android and it doesn't cover the questions I asked. Telling me to study harder doesn't help when the answer isn't in the books. If you're such an expert just answer the questions and help people. Spend more time answering the questions and less time belittling people. Your post is not helpful. – Matt Jun 09 '18 at 15:26
  • Thanks YCF. So since Drawer is made up of Tools do I have to instantiate tools before using Drawer? – Matt Jun 09 '18 at 15:35
  • It might be in some book, but it's not in mine. https://www.amazon.com/Android-Application-Development-All-Dummies/dp/1118973801 – Matt Jun 09 '18 at 16:03
  • I would recommend that you turn to the original Java tutorials from oracle. Read through them top to bottom. – GhostCat Jun 09 '18 at 16:07
  • GhostCat, this board would be pretty useless if every answer only consisted of "read the entire Oracle documentation on Java", or "read the entire Google documentation on Android", or "go read the entire Internet". These are lazy deflections and not productive for anyone. Thank you YCF and Leo for posting helpful answers. I'm trying them now. – Matt Jun 09 '18 at 16:40

2 Answers2

2

Here's a more standard way to do it.

public class Tool {
    final String name;
    final double weight;

    public Tool(String name, double weight) {
        this.name = name;
        this.weight = weight;
    }
}

public class Drawer {
    final String name;
    final List<Tool> tools;

    public Drawer(String name) {
        this.name = name;
        this.tools = new ArrayList<>();
    }
}

public class Toolbox {
    final String name;
    final List<Drawer> drawers;

    public Toolbox(String name) {
        this.name = name;
        this.drawers = new ArrayList<>();
    }

    public void addDrawers(String... names) {
        for (String name : names) {
            drawers.add(new Drawer(name));
        }
    }
}

Now you can do

String[] drawers = {"Screwdrivers", "Files", "Spanners"};
Toolbox toolbox = new Toolbox("My toolbox");
toolbox.addDrawers(drawers);

Or just

Toolbox toolbox = new Toolbox("My toolbox");
toolbox.addDrawers("Screwdrivers", "Files", "Spanners");
Leo Aso
  • 11,898
  • 3
  • 25
  • 46
1

I'm getting null pointer issues.

In myObject.callAMethod(aParameter); check myObject is not null. Also refer to What is a NullPointerException, and how do I fix it?

What's the proper way to instantiate everything if I want to instantiate a toolbox?

Each class can have it's own file. If you are starting with java I would go this way. There are ways to put everything in the same file, but you'll get to that in time. The propper way to instantiate as in which order, what makes sense is from the bottom up. First Tool, then Drawer, then ToolBox. In this way the encolosed element already exists when you create the enclosing object.

do I need to use the import statement

Depends in which package you create the different classes. The IDE will highlight if you need to import the class.

I've read an entire book on programming Java on Android and it doesn't cover the questions I asked.

This cannot be true.

Juan
  • 5,525
  • 2
  • 15
  • 26