0

I have a homework about implementing queues in java. I have written a code, but there is an error and I don't know how to fix it. Can please anyone help me with this?

Here is my code:

public class Radha {

    public int num;
    public Radha pas;

    public Radha(int num){

        this.num = num;

    }   

    public void shfaq(){

        System.out.println(num);

    }

    public static void main (String [] args){

        Radha x = new Radha(1);
        Radhe1 r = x.new Radhe1();
        r.enqueue(1);
        r.shfaq();


    }

    class Radhe1{

        public Radha koka;
        public Radha bishti;

        Radhe1(){

            koka.pas = null;
            bishti = koka;

        }

        public void shfaq(){

            Radha theLink = koka;

            while(theLink != bishti){

                theLink.shfaq();

                theLink = theLink.pas;

            }

        }

        public boolean bosh(){

            return(bishti == koka);

        }

        public int iPari (){
            if (bosh())
                System.out.println("radha eshte bosh");
                    return(koka.num);
        }

        public void dequeue(){

            if (bosh()){
                System.out.println("radha eshte bosh");
            }
            else{
                koka = koka.pas;
            }

        }

        public void enqueue(int a){

            bishti = bishti.pas;
            bishti.num = a;
            bishti.pas = null;

        }

    }

}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Adela
  • 35
  • 1
  • 8
  • You haven't shown for us the error, nor indicated what line causes it -- So how are we supposed to help? Please correct this. – Hovercraft Full Of Eels Jun 08 '15 at 01:03
  • When I run it, it says: Exception in thread "main" java.lang.NullPointerException at Radha$Radhe1.(Radha.java:36) at Radha.main(Radha.java:22) – Adela Jun 08 '15 at 01:04
  • If you've done even a little searching on solving a NullPointerException (NPE), you'll know that the most important bit of information that we need is the exception's associated stacktrace and some identification of the line that causes it, something that the stacktrace will tell you, and unfortunately neither of which you've posted here with your question. Please fix this so that we can help you. All this information should be part of your original post -- please edit your question. – Hovercraft Full Of Eels Jun 08 '15 at 01:04
  • Also, you will want to understand the general concepts of how to debug a NPE (NullPointerException). **You should critically read your exception's stacktrace to find the line of code at fault, the line that throws the exception, and then inspect that line carefully**, find out which variable is null, and then trace back into your code to see why. You will run into these again and again, trust me. – Hovercraft Full Of Eels Jun 08 '15 at 01:05

1 Answers1

0

When you write koka.pas = null, there is no koka whose pas you can set. You must initialize that somehow.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413