1

my professor refuses to tell me what I'm doing wrong and I for the life of my can't figure it out. I'm just starting java so I may look foolish but

import java.util.Scanner;

public class Library {
    MediaItem[] items = new MediaItem[100];
    int numberOfItems = 0;
    static int displayMenu(){
        @SuppressWarnings("resource")
        Scanner dog = new Scanner(System.in);
        System.out.println("Menu:\n1:Add New Item\n2:Mark Item as Loaned\n3:List All Items\n4:Mark Item as Returned\n5:Quit");
    int cat = dog.nextInt();
    return cat;
}
void addNewItem(String title, String format){
    MediaItem item = new MediaItem();
    item.setTitle(title);
    item.setFormat(format);
    items[numberOfItems] = item;
    numberOfItems++;
}
void markItemOnLoan(String title, String name, String date){
    for (int i=0;i<numberOfItems;i++){
        MediaItem item = items[i];
        String title5 = item.getTitle();
        if (title5 == title){
            item.markOnLoan(name, date);
            i=numberOfItems;
        }

        }
}
String[] listAllItems(){
    String[] all = new String[numberOfItems];
    for (int i=0;i<numberOfItems;i++){
        MediaItem item = items[i];
        if (item.onLoan=true){
            all[i]=item.getTitle()+" "+item.getFormat()+" "+item.getOnLoan()+" "+item.getLoanedTo()+" "+item.getDateLoaned()+" ";
        }else{
            all[i]=item.getTitle()+" "+item.getFormat()+" "+item.getOnLoan()+" ";
        }

    }
    return all;
}
void markItemReturned(String title){
    for (int i=0;i<numberOfItems;i++){
        MediaItem item = items[i];
        String title1 = item.getTitle();
        if (i==numberOfItems){
            System.out.println("Title not found.");
        }else if (title1 == title){
            item.markReturned();
            i=numberOfItems;
        }
        }
}

public static void main(String[] args) {
    Library banana = new Library();
    String invalid = "Invalid Option";
    @SuppressWarnings("resource")
    Scanner dog = new Scanner(System.in);
    for (int i=0; i==0;){
        int cat = displayMenu();
        if (cat<=0){
            System.out.println(invalid);
        }
        if (cat>=6){
            System.out.println(invalid);
        }
        switch (cat){
        case 1:
            System.out.println("What is the title of the item?");
            String title = dog.nextLine();
            System.out.println("What is the format of the item?");
            String format = dog.nextLine();
            banana.addNewItem(title, format);
            break;
        case 2:
            System.out.println("What is the title of the item?");
            String title1 = dog.nextLine();
            System.out.println("What is your name?");
            String name = dog.nextLine();
            System.out.println("What is the date?");
            String date = dog.nextLine();
            banana.markItemOnLoan(title1, name, date);
            break;
        case 3:
            String[] list = banana.listAllItems();
            System.out.println("List of all items:\n");
            for (int o=0;o<banana.numberOfItems;o++){
                System.out.println(list[o]);
            }
            break;
        case 4:
            System.out.println("What is the title you are returning?");
            String title2 = dog.nextLine();
            banana.markItemReturned(title2);
            break;
        case 5:
            i++;
            break;
        }
    }
}
}

I'm having this problem throughout my code but I figure if you guys can answer this one I can figure the rest out on my own. Edit: I'm getting further, now I just need to figure out why it isn't reading the name and date.

  • Sorry but the topic has no connection with the description – Nabin Oct 23 '14 at 17:16
  • And for this MediaItem class needs to be posted – Nabin Oct 23 '14 at 17:16
  • 1
    Look here usually you get this error when you would call a non static method from a static block. Now the question is why and how does that matter? For that you need to learn about class loading and what happens during class loading, what are the variables or methods which gets loaded first etc. I suppose I have given you enough hints. Good luck. – User27854 Oct 23 '14 at 17:18
  • 1
    Please tell me those are two snippets of unrelated code. If your method `Library` isn't static, you need an instance of whatever class has the method to call it (or you make the method `static`). Regardless, you haven't posted sufficient code to say more. – Elliott Frisch Oct 23 '14 at 17:18
  • I posted the rest of the Library class, if you need the MediaItem class let me know. – Tim Koehler Oct 23 '14 at 17:32
  • You need an instance of Library to call the method on. You're calling addNewItem from a static method. –  Oct 23 '14 at 17:38

1 Answers1

0

In your switch statement, you call addNewItem, which is not declared static.

In order to call addNewItem, you must have an Object to call it from, unless you declare it static, or call obj.addNewItem(). You didn't include much code, so I'm not sure what class the object should be.

static void addNewItem(String title, String format){
    MediaItem item = new MediaItem();
    item.setTitle(title);
    item.setFormat(format);
    items[numberOfItems] = item;
    numberOfItems++;
}

the static keyword means that there is only a single instance of the method, field, or class even if there isn't an instance of the class associated with it. Without the static modifier, there needs to be an instance of the class to access the method, field, or class from.

For a good example, see: https://stackoverflow.com/a/15828090/1732480

Edit: After you posted more code, you have no instance of Library to call addNewItem on. Create a Library with Library l = new Library(); and call l.addNewItem()

public static void main(String[] args) {
    Library l = new Library();
    String invalid = "Invalid Option";
    Scanner dog = new Scanner(System.in);
    for (int i=0; i==0;){
        int cat = displayMenu();
        if (cat<=0){
            System.out.println(invalid);
        }
        if (cat>=6){
            System.out.println(invalid);
        }
        switch (cat){
            case 1:
                System.out.println("What is the title of the item?");
                String title = dog.nextLine();
                System.out.println("What is the format of the item?");
                String format = dog.nextLine();
                l.addNewItem(title, format);
                break;
         ....
Community
  • 1
  • 1
  • Well after using your advice I am now getting the error message 'Exception in thread "main" java.lang.NullPointerException at midterm.Library.markItemOnLoan(Library.java:25) at midterm.Library.main(Library.java:88)' when I get to the "markItemAsLoaned" – Tim Koehler Oct 23 '14 at 17:49
  • That's another bug in your code. I'm not going to do all of your homework for you. ;) It looks like you're using eclipse, set a breakpoint in your code on the method `markItemAsLoaned`, and step through the code to check the values of each variable. Hint: I have a feeling one of the items was not set to a correct value. Also, is there any reason you're incrementing at the end of the loop instead of in the for statement (the error is in the loop. < vs <=)? –  Oct 23 '14 at 17:57
  • It won't read the name and date. Nothing I do fixes it. I figured if I put the i++ at the end of the loop it'd keep it from messing up it's connection with "numberOfItems" – Tim Koehler Oct 23 '14 at 19:09
  • @TimKoehler I feel you're branching into more questions than originally asked. This is not so much a question, but an attempt at crowd-sourcing bug fixes. You don't have enough information attached to thoroughly figure out what's happening. Attach the input, output, and a stacktrace as well, and I might be able to help you. –  Oct 23 '14 at 19:24
  • It appears there's more problems with the code than what I originally thought. – Tim Koehler Oct 24 '14 at 02:25
  • It's a media library that you input each item. Each item has a holds a string for title, format, who the item is loaned to, and the date it was loaned, and a boolean for a loan. I don't know what a stacktrace is. – Tim Koehler Oct 24 '14 at 02:34
  • When the error occurs, copy the entire block of text it outputs, that says the error and where it occurred, and paste it into your question. –  Oct 24 '14 at 02:37
  • I figured out that the problem is in the markItemOnLoan method. How should I go about comparing the title from the input with the title from the media item? – Tim Koehler Oct 24 '14 at 02:57
  • Are you using eclipse? –  Oct 24 '14 at 02:58
  • It's not a syntax error, I think it's a logic error. Yes I am. – Tim Koehler Oct 24 '14 at 03:04
  • I understand its a logic error, it wouldnt compile otherwise. Right click and set a breakpoint in the method the issue occurs in. Instead of "Run..", click debug. Use the buttons on the top to step through the code and hover over variables to view their values. Debugging is a valuable skill. Read a tutorial on debugging in eclipse. –  Oct 24 '14 at 03:14
  • Apparently title5 and title don't == eachother, can strings not equal eachother? – Tim Koehler Oct 24 '14 at 06:18
  • For strings, use .equals and .equalsIgnoreCase –  Oct 24 '14 at 16:02
  • @TimKoehler Common practice is `str.trim().equals(otherStr.trim())`, which strips whitespace from the beginning and the ends of the strings and then compares the contents of each. –  Oct 24 '14 at 19:47