0
package arrays;

import java.util.Arrays;

public class Route {

int cityindex;
int stadtwahl;
String[] cities = {"Berlin", "Hamburg", "Kiel", "Muenchen", "Stuttgart", "Dresden", "Heidelberg"};
int stadtwahlA;
int stadtwahlB;
int[][] entfernung;
int A;
int B;


public void Route() {

    entfernung = new int[A][B];
    this.A = A;
    this.B = B;
    entfernung[0][1] = entfernung [1][0] = 288;
    entfernung[0][2] = entfernung [2][0] = 355;
    entfernung[0][3] = entfernung [3][0] = 585;
    entfernung[0][4] = entfernung [4][0] = 632;
    entfernung[0][5] = entfernung [5][0] = 194;
    entfernung[0][6] = entfernung [6][0] = 628;


}

    public void einlesen() {


        System.out.println("Sie haben folgende Staedte zur Auswahl: " 
        + "\n0: " + cities[0] //Berlin
        + "\n1: " + cities[1] //Hamburg
        + "\n2: " + cities[2] //Kiel
        + "\n3: " + cities[3] //Muenchen
        + "\n4: " + cities[4] //Stuttgart
        + "\n5: " + cities[5] //Dresden
        + "\n6: " + cities[6] + "\n"); //Heidelberg

        System.out.println("Bitte waehlen Sie eine Stadt: ");
        stadtwahlA = Konsole.getInputInt();
        System.out.println("Bitte waehlen Sie ein Ziel: ");
        stadtwahlB = Konsole.getInputInt();

    }

        public void ausgabe() {
            System.out.println("Die Entfernung zwischen " + cities[stadtwahlA] + " und " + cities[stadtwahlB] + " betraegt : " );
            this.A = stadtwahlA;
            this.B = stadtwahlB;
            System.out.print(entfernung[0][1]);
            }
}

my idea was to print the distance (entfernung) between some cities. so for testing reasons i just entered the distance between the city 0 (berlin) and the others. eventually i want to write

 System.out.print(entfernung[stadtwahlA][stadtwahlB]);

or at least that seemed to be the best choice for me - but for some reason, i get a nullpointerexception when running this code:

Sie haben folgende Staedte zur Auswahl: 0: Berlin 1: Hamburg 2: Kiel 3: Muenchen 4: Stuttgart 5: Dresden 6: Heidelberg

Bitte waehlen Sie eine Stadt: 1 Bitte waehlen Sie ein Ziel: 5 Die Entfernung zwischen Hamburg und Dresden betraegt : Exception in thread "main" java.lang.NullPointerException at arrays.Route.ausgabe(Route.java:70) at arrays.RouteTest.main(RouteTest.java:8)

since i am a newbie and all, i'd be very thankful for any help thats explaining me where my fault is as easy as possible ;) thanks in advance!

edit: this is my test class:

    package arrays;

public class RouteTest {

    public static void main(String[] args) {
        Route R1 = new Route();
        R1.einlesen();
        R1.ausgabe();


    }

}
5
  • 1
    Please post all your code so I can run the program. There is no main method in the code you listed. Commented Dec 13, 2015 at 15:48
  • What do you think A and B is in entfernung = new int[A][B]; this.A = A; this.B = B;? Commented Dec 13, 2015 at 15:49
  • @Pshemo oh, this was just a tryout, a lucky shot to see if it would make any difference... Commented Dec 13, 2015 at 15:57
  • @JoshChappelle i have edited the post with my test class Commented Dec 13, 2015 at 15:58
  • What is Konsole? The code compiles except for that. Commented Dec 13, 2015 at 16:00

1 Answer 1

2

The problem you are having is because you have void in front of what you mean to be a constructor in Route. So entfernung is never getting initialized. It's null.

Change

public void Route() {

To this

public Route() {

The next error you will hit is an ArrayIndexOutOfBoundsException because your constructor is declaring a 0 by 0 size array and then you are trying to reference the [0][1] element of that array which doesn't exist. That's because you haven't initialized the A and B variables. I'll leave that to you as an excercise.

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

7 Comments

nice, thanks. and you were right, my next problem IS an indexoutofbounds... ill try to figure it out for now and maybe come back later. thanks so far!
Great! Please accept the answer if that solved your problem.
will the tread then be closed? what i have further questions? to be honest, dont know how to solve the new arrayindexoutofboundsexception... ive been coding the whole day and i am totally tired, but i have to get this stuff done by tomorrow.
No it won't be closed. It just marks the answer as accepted and gives me credit for the answer. If you have another question I suggest creating a new thread for that.
for a good reason i'm not allowed to post another question within 90 minutes - can you give me a hint in this thread?
|

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.