0

I am new in java and trying to learn it.I am getting this error.

Exception in thread "main" java.lang.NullPointerException at ödev.Ogrenci.main(Ogrenci.java:145)

This is my homework. It is about calculating notes of the random students.

PS: I am Turkish and if some words in the code make no sense to you, ignore it. Those words are probably Turkish. :)

package ödev;



public class Ogrenci {



public int no;
public String ad;
public String soyad;

private int vize1;
public int getVize1() {
    return vize1;
}
public void setVize1(int vize1) {
    this.vize1 = vize1;
}
public int getVize2() {
    return vize2;
}
public void setVize2(int vize2) {
    this.vize2 = vize2;
}
public int getFinal() {
    return Final;
}
public void setFinal(int final1) {
    Final = final1;
}
private int vize2;
private int Final;

String[] harf = new String[]{"a", "e", "i", "h", "k", "l", "m", "n"};

public Ogrenci(){



    }

public void randomNot(){

    int i1 = (int)(Math.random()*11);

    int i2 = (int)(Math.random()*101);

    int i3 = (int)(Math.random()*11);


    if(i1<8)
        vize1 = (int)(Math.random()*36+65);
    else
        vize1 = (int)(Math.random()*65);

    if(i2<26)
        vize2 = (int)(Math.random()*51+50);
    else
        vize2 = (int)(Math.random()*50);

    if(i3<8)
        Final = (int)(Math.random()*36+65);
    else
        Final = (int)(Math.random()*65);

}



public String ad(){

    String isim = "";

    int a1 = (int)(Math.random()*6);
    int a2 = a1+3;

    for(int i = 0; i<a2; i++){
        int a3 = (int)(Math.random()*8);

        String a = harf[a3];

        isim += a;
        }

    return isim;

    }

public String soyad(){

        String isim = "";

        int a1 = (int)(Math.random()*6);
        int a2 = a1+3;

        for(int i = 0; i<a2; i++){
            int a3 = (int)(Math.random()*8);

            String a = harf[a3];

            isim += a;
            }

        return isim;

        }

public String harfnotu(){

    double ort = (0.3*vize1)+(0.3*vize2)+(0.4*Final);

    if(ort>=0 && ort<50)
        return "FF";
    else if(ort>=50 && ort<55)
        return "DD";
    else if(ort>=55 && ort<60)
        return "DC";
    else if(ort>=60 && ort<70)
        return "CC";
    else if(ort>=70 && ort<75)
        return "CB";
    else if(ort>=75 && ort<85)
        return "BB";
    else if(ort>=85 && ort<90)
        return "BA";
    else if(ort>=90 && ort<=100)
        return "AA";
    else if(ad.equals("hakan"))
        return "FF";

    return "Harf notu hesaplanamamistir.";
}




public static void main(String[] args){


        Ogrenci[] Ogt =  new Ogrenci[30];

        for(int z=0; z<30; z++){

            Ogt[z].ad();
            Ogt[z].soyad();
            Ogt[z].randomNot();
            Ogt[z].no = 0710000 + z ;

        }





        System.out.println("Numara         Ad   Soyad             1.vize -- 2.vize -- Final -- Harf Notu");

        for(int k = 0; k<30; k++){

            System.out.println(Ogt[k].no+" "+Ogt[k].ad()+" "+Ogt[k].soyad()+" "+Ogt[k].vize1+" "+Ogt[k].vize2+" "+Ogt[k].Final+" "+Ogt[k].harfnotu());

        }








}


}
3
  • Which line is line 145? Commented Nov 25, 2013 at 20:36
  • 2
    Welcome to StackOverflow! Instead of posting all your code, please post the section where you think the error is. You are welcome to include any other pertinent code as well. Please also post the complete stacktrace along with your own attempts to solve the problem. Commented Nov 25, 2013 at 20:36
  • Ogrenci[] Ogt = new Ogrenci[30]; for(int z=0; z<30; z++){ Ogt[z].ad(); "Ogt[z].ad();" is the line 145. Commented Nov 25, 2013 at 20:38

5 Answers 5

2

You didn't initialize any of the Ogrenci objects, just allocated an array of them. They're all null until you initialize them. This should fix it:

    Ogrenci[] Ogt =  new Ogrenci[30];

    for(int z=0; z<30; z++){
        Ogt[z] = new Ogrenci();
        Ogt[z].ad();
        Ogt[z].soyad();
        Ogt[z].randomNot();
        Ogt[z].no = 0710000 + z ;

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

2 Comments

i made a very dumb mistake :) thanks for your help .
@user3033875 It's not a problem at all. Please consider upvoting all of the other answers that match mine. We all came to the same conclusion and the other posters deserve as much credit as I do. Good luck!
2

In main, you have this block:

Ogrenci[] Ogt =  new Ogrenci[30];

    for(int z=0; z<30; z++){

        Ogt[z].ad();
        Ogt[z].soyad();
        Ogt[z].randomNot();
        Ogt[z].no = 0710000 + z ;

    }

You declared your array, but the array is initialized to all nulls. Create your Ogrenci objects before you access them:

for(int z=0; z<30; z++){
    Ogt[z] = new Ogrenci();
    // Then call methods on it here.

Incidentally, be careful with numeric literals starting with 0, because that indicates on octal number (unless, of course, that's what you intended).

Comments

2

The problem is here, in your main function:

Ogrenci[] Ogt =  new Ogrenci[30];

    for(int z=0; z<30; z++){

        Ogt[z].ad();
        Ogt[z].soyad();
        Ogt[z].randomNot();
        Ogt[z].no = 0710000 + z ;

    }

You have declared that there are 30 Ogrenci, but you do not initialize any of them.

use:

   Ogrenci[] Ogt =  new Ogrenci[30];

    for(int z=0; z<30; z++){
        Ogt[z] = new Ogrenci();

        Ogt[z].ad();
        Ogt[z].soyad();
        Ogt[z].randomNot();
        Ogt[z].no = 0710000 + z ;

    }

Comments

1
Ogrenci[] Ogt =  new Ogrenci[30];

With above statement, You have just declared an array which can contain the instance of Ogrenci, where any element ogt[i] is an instance of Ogrenci and initialized to null. You will have to create each element of the array before accessing them using new.

for(int z=0; z < Ogt.length; z++) //<--- don't use numerical value directly(30) 
                                 //while visiting whole array
{
    Ogt[z] = new Ogrenci();
    // other relevant code
}

Comments

0

You instantiate Ogt but then before populating it with anything trying to access it's contents Ogt[z].ad(), there is nothing inside hence NPE

Comments

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.