How to set the values base on a class

Not sure whether my title is correct or not. I create a variable based on the datatype of the class. I have listed some of the example as below.

I created 3 classes:

  1. testing.java
  2. Sentence.java
  3. Entity.java

I am new to java and wanted to learn.

Testing.java

package test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.*;

public class testing {

    private static Entity ent; 


    public static void main(String[] args) 
    { 
      ent.setStart(1);
      ent.setBegin(2);
      Entity entity = ent.setStart(start); # My question is in this part
      List<String> sentences = new ArrayList<String>(Arrays.asList("I have a thing", "I have nothing"));
      List<String> context = sentences.subList(0,sentences.size() - 1);
      String target = sentences.get(sentences.size() -1 );

    } 

}

Sentence.java

package test;

import java.util.List;

public class Sentence {

    private List<Entity> entities;

    public List<Entity> getEntities() {
        return entities;
    }

    public void setEntities(List<Entity> entities) {
        this.entities = entities;
    }

}

Entity.java

package test;


public class Entity {

    private Integer begin;

    private Integer start;

    public Integer getBegin() {
        return begin;
    }

    public void setBegin(Integer begin) {
        this.begin = begin;
    }

    public Integer getStart() {
        return start;
    }

    public void setStart(Integer start) {
        this.start = start;
    }

}

I do not understand what to do to create a variable for entity. I have been trying but it wont work.

I also wanted to know why shouldn't I import Entity.java and Sentence.java into Testing.java? I thought we must import it in order to use it? Somehow it worked without the use of import.

Upvotes: 0

Views: 207

Answers (1)

Tarmo
Tarmo

Reputation: 4081

I also wanted to know why shouldnt I import Entity.java and Sentence.java into Testing.java? I thought we must import it in order to use it?

You only have to import stuff that is in a different package. Your stuff is all in package called test so you don't need to import it.

About the problem itself. You are probably getting a NullPointerException because you are not initializing your static variable ent

In the first line of your main method, you should initialize it like so: ent = new Entity();

Also this part does not make any sense at all: Entity entity = ent.setStart(start);

ent.setStart(start) returns void not an Entity.

So to conclude, something like this should be at least compilable:

public static void main(String[] args) { 
      ent = new Entity();
      ent.setStart(1);
      ent.setBegin(2);
      int start = 0;
      ent.setStart(start);
      List<String> sentences = new ArrayList<String>(Arrays.asList("I have a thing", "I have nothing"));
      List<String> context = sentences.subList(0,sentences.size() - 1);
      String target = sentences.get(sentences.size() -1 );

} 

Upvotes: 1

Related Questions