0
        //Main : 
        package DevAyo;

        import java.util.Scanner;

        public class Main {
         private static Scanner scanner =new Scanner(System.in);
         private static Album Alb =new Album("Kamikaze");
            public static void main(String[] args) {
              boolean quit = true;
              while (quit){
                System.out.println("Choose actions : ");
                System.out.println("\t\t 1.Add new Songs to Album :");
                System.out.println("\t\t 2.printSongs");
                System.out.println("\t\t 3.Quit");
                System.out.println("choose Action : ");
                int choice = scanner.nextInt();
                scanner.nextLine();
                switch (choice){
                    case 1:
                        AddSong();
                        break;
                    case 2:
                        Alb.printSongs();
                        break;
                    case 3:
                        quit=false;
                        break;
                }
            }
            }
            public static void AddSong(){
        //        System.out.println("Insert Album Name:  ");
        //        String albumName =scanner.nextLine();
                System.out.println("Pleas Enter Number of Songs :");
                int Number = scanner.nextInt();
                for (int i=0;i<Number;i++){
                    System.out.println("Enter Song Name "+(i+1)+": ");
                    String name=scanner.nextLine();
                    scanner.nextLine();
                    System.out.println("Enter Singer of Song "+(i+1)+": ");
                    String SingerName =scanner.nextLine();
                    System.out.println("Enter Song "+(i+1)+" released Day: ");
                    String ReleasedDay = scanner.nextLine();
                    System.out.println("Enter Length of the  Song"+(i+1)+": ");
                    int SongLength=scanner.nextInt();
                    Songs song = Songs.creatSong(name,SingerName,ReleasedDay,SongLength);
                   if(Alb.addSOngs(song))
                       System.out.println("Songs added ! ");
                   else
                       System.out.println("Error ! ");
                }
            }`enter code here`
        }
    //Album Class
    package DevAyo;

    import java.util.ArrayList;
    import java.util.Scanner;

    public class Album {
        public  String AlbumName;
        private static Scanner scanner = new Scanner(System.in);
        ArrayList<Songs> ListofSongs;

        public Album(String albumName) {
            AlbumName = albumName;
            this.ListofSongs=new ArrayList<Songs>();
        }

        public boolean addSOngs(Songs song){
            if(findSong(song.getSongName())>=0){
                System.out.println("Songs aLready Exisit ! ");
                return false;
            }
            ListofSongs.add(song);
            return true;
     }

     private int findSong(String songName){
            for(int i=0; i<ListofSongs.size();i++){
                Songs song = ListofSongs.get(i);
                if(song.getSongName().equals(songName)){
                    return i;
                }

            }
         return -1;
     }

     public void printSongs(){
            for(int i=0;i<this.ListofSongs.size();i++){
                System.out.println("Song Name: "+ this.ListofSongs.get(i).getSongName()+"\n"
                +"Singer: "+this.ListofSongs.get(i).getSongSinger()+"\n"+"Song Released Day: "+this.ListofSongs.get(i).getSongDay()+
                        "\n"+"Song Length: "+this.ListofSongs.get(i).songLength+" min");
            }
     }

    }

//Songs Class

package DevAyo;

public class Songs {
    public static String songName;
    public static String songSinger;
    public static String songDay;
    public static int songLength;

    public Songs(String songName, String songSinger, String songDay, int songLength) {
        this.songName = songName;
        this.songSinger = songSinger;
        this.songDay = songDay;
        this.songLength = songLength;
    }

    public String getSongName() {
        return this.songName;
    }

    public String getSongSinger() {
        return this.songSinger;
    }

    public String getSongDay() {
        return this.songDay;
    }

    public int getSongLength() {
        return this.songLength;
    }


    public static Songs creatSong(String name, String singerName, String releasedDay, int songLength){
        return new Songs(songName,songSinger,songDay,songLength);
    }
}

i just tried so many was i can't undrestand why it displlays null , i 've created the constructor in both classes and still dosen't seem to work, honestly i've searched the internet about the problem but it's says that i should add constructor i've add them but nothing happpen anyway hope the code is clear , hope you guys can help thanks .

  • 1
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Patrick Hollweck Feb 20 '19 at 18:05

1 Answers1

3

After a Scanner.nextInt(); leaves an empty line so the next time you call Scanner.nextLine(); it will give you that empty line. If you want to avoid this, you could do one of this 2 options:

  1. Parse

    Always use Scanner.nextLine() and cast to the type of variable you want. Example: int a = Integer.parseInt(scanner.nextLine());

  2. Read that empty line

    always you use the nextInt() immediately call nextLine() Example: int a = scanner.nextInt(); scanner.nextLine()