Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

The following program I'm creating will allow the user will input values to create an array of videos. Each video contains several data fields (number, title, publisher, duration & date). The program is primarily controlled through a switch menu. However after creating the array, choosing another option, such as Show videos will throw a Null Pointer Exception. These exceptions usually occur when you haven't assigned values to the array, but that should have been already done in the first option, createLibrary(). Other functions in Search videos and Change videos have the same issue and it occurs every time a get method is used.

Anyone with a reasonable practical answer will greatly help.

Here is a segment of my code:

import java.util.*;
public class EnterLibrary
{
public final int MAX_ITEMS = 5;
public Library[] videos;
public int size = 0;

public EnterLibrary()
    {
        videos = new Library[MAX_ITEMS];
        java.util.Scanner scannerObject =new java.util.Scanner(System.in);
        LibraryMenu Menu = new LibraryMenu();
        Menu.displayMenu();
        switch (scannerObject.nextInt())
        {
            case 1:
            System.out.println ("1 - Add Videos");
                if (size < MAX_ITEMS) {
                Library video = createLibrary();
                videos[size++] = video;
                }
            new EnterLibrary();
            break;
            case 2:
            System.out.println ("2 - Show Videos");
            printVidLibrary(videos);
            new EnterLibrary();
            break;
            case 3:
            System.out.println ("3 - Search Videos");
            searchLibrary(videos);
            new EnterLibrary();
            break;
            case 4:
            System.out.println ("4 - Change Videos");
            changeLibrary(videos);
            break;      
            case 5:
            System.out.println ("5 - Delete Videos");
            deleteVideo(videos);
            new EnterLibrary();
            break;
            default:
            System.out.println ("Unrecognized option - please select options 1-5 ");
            break;
        }
    }

public Library createLibrary()
{
    Library video = new Library();
    java.util.Scanner scannerObject =new java.util.Scanner(System.in); 
    for (int i = 0; i < videos.length; i++)
    {
    //User enters values into set methods within the Library class
    System.out.print("Enter video number: " + (i+1) + "\n");
    String number = scannerObject.nextLine();
    System.out.print("Enter video title: " + (i+1) + "\n");
    String title = scannerObject.nextLine();
    System.out.print("Enter video publisher: " + (i+1) + "\n");
    String publisher = scannerObject.nextLine();
    System.out.print("Enter video duration: " + (i+1) + "\n");
    String duration = scannerObject.nextLine();
    System.out.print("Enter video date: " + (i+1) + "\n");
    String date= scannerObject.nextLine();
    System.out.print("VIDEO " + (i+1) + " ENTRY ADDED " + "\n \n");
    //Initialize arrays
    videos[i] = new Library ();
    videos[i].setVideo( number, title, publisher, duration, date  );
    }
    return video;
}

public void printVidLibrary( Library[] videos)
{
    //Get methods to print results
    System.out.print("\n***VIDEO CATALOGUE*** \n");
    for (int i = 0; i < videos.length; i++)
    {
    System.out.print("Video number " + (i+1) + ": \n" + videos[i].getNumber() + "\n ");
    System.out.print("Video title " + (i+1) + ": \n" + videos[i].getTitle() + "\n ");
    System.out.print("Video publisher " + (i+1) + ": \n" + videos[i].getPublisher() + "\n ");
    System.out.print("Video duration " + (i+1) + ": \n" + videos[i].getDuration() + "\n ");
    System.out.print("Video date " + (i+1) + ": \n" + videos[i].getDate() + "\n ");
    }
}
//Code for other functions not displayed here

public static void main(String[] args)
    {

    new EnterLibrary();
    }
}

The exception error:

Exception in thread "main" java.lang.NullPointerException
        at EnterLibrary.printVidLibrary(EnterLibrary.java:80)
        at EnterLibrary.<init>(EnterLibrary.java:26)
        at EnterLibrary.<init>(EnterLibrary.java:22)
        at EnterLibrary.<init>(EnterLibrary.java:22)
        at EnterLibrary.<init>(EnterLibrary.java:22)
        at EnterLibrary.<init>(EnterLibrary.java:22)
        at EnterLibrary.main(EnterLibrary.java:202)
share|improve this question
2  
Use a debugger and see what you see. –  bmargulies Jun 19 '12 at 0:27
add comment

3 Answers

up vote 3 down vote accepted

There are at least two serious errors in your code:

  1. There's a stack overflow waiting to happen: you're calling EnterLibrary() inside EnterLibrary(), that will produce an infinite recursion
  2. Although you have instantiated the videos array, you haven't initialized it yet ant it's full of null objects; make sure to call createLibrary() at the beginning of EnterLibrary's constructor, before calling printVidLibrary(), which iterates over all the contents of videos and assumes that it's entirely initialized
share|improve this answer
1  
I decided remove the createLibrary function from the switch statement and moved it to the main method before the switch menu. This way makes it work now. This time I made it that you add the values before displaying the switch menu. Cheers –  Shane Jun 19 '12 at 0:58
add comment

videos = new Library[MAX_ITEMS]; doesn't initialize the Library items, so each of them is null. Try this

videos = new Library[MAX_ITEMS];
for(int x = 0; x < MAX_ITEMS; x++)
  videos[x] = new Library();

You can also check in printVidLibrary if the videos argument is null to avoid the exception.

share|improve this answer
add comment

You got it wrong, you have a twisted mind !

If I understand you want to go over your switch again (after adding a video for example) : you should use a loop around your switch, not create a new, hence video-virgin "EnterLibrary" object.

Some code :

while (i = scannerObject.nextInt()) {
  switch(i) {
    case 1:
        System.out.println ("1 - Add Videos");
        if (size < MAX_ITEMS) {
          Library video = createLibrary();
          videos[size++] = video;
        }
        break;
    case 2:
        System.out.println ("2 - Show Videos");
        printVidLibrary(videos);
        break;
    case 3:
        System.out.println ("3 - Search Videos");
        searchLibrary(videos);
        break;
    case 4:
        System.out.println ("4 - Change Videos");
        changeLibrary(videos);
        break;      
    case 5:
        System.out.println ("5 - Delete Videos");
        deleteVideo(videos);
        break;
    default:
        System.out.println ("Unrecognized option - please select options 1-5 ");
        break;
    }
}

On a side note, I haven't given it much thought and I'm kind of tired right now, but this doesn't feel like an overall good use of OOP, I might be wrong but I can't get a catch of what an "EnterLibrary" object would be, for starters.

And yep, get familiar with the debugger as suggested by bmargulies, he's your best friend I guess

edit: I edited the code, forgot to remove your recursive call

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.