0

I'm trying to output the max value from a double array using a code sample and I cannot find why the values are being thrown as null, im sorry this is obvious but to my eyes the values have info, or I am misunderstanding something.

I have tried to insert;

 int maximum = t[0];   // start with the first value
for (int i=1; i<t.length; i++) {
    if (t[i] > maximum) {
        maximum = t[i];   // new maximum
    }
}

This is my code:

package test;



import java.util.Scanner;

public class projectone {

public static void main(String[] args) {



// Prepares keyboard as input device 
    Scanner keyboard = new Scanner (System.in);

// Declare variables for user information
    String[] title = new String[20];
    String[] author = new String[20];
    String[] publisher = new String[20];
    Double[] price = new Double[20]; 
    int[] pages = new int[20];
    String[] isbn = new String[20];     

// Declare variables for system information
    int index;
    index = 0;
    int totalbooks;
    totalbooks = 0;
    String temp;

    double sum = 0;
    double total= 0;        

// Loop to prompt user to enter information
    for (index = 0; index < 20; index++)
    {
            System.out.println("Please enter book details,");
            System.out.println("when complete please enter 'nomore' as 'Title'");
            System.out.println("Title :");
            title[index] = keyboard.nextLine();
            if (title[index].contentEquals("nomore"))
            {
                break; //this ends the loop if the user enters 'nomore' in the title field, should
                        // this work in every field?
            }
            System.out.print("Author :");
            author[index] = keyboard.nextLine();                
            System.out.print("Publisher :");
            publisher[index] = keyboard.nextLine(); 

        // The price field is converted from a string to a double, then added to the total count of all
        // previous books prices
            System.out.println("Price :");
            temp = keyboard.nextLine();
            total = Double.valueOf(temp);
            price[index] = Double.valueOf(temp);

            sum = sum += total;

            System.out.print("Page Count :");
            temp = keyboard.nextLine();
            pages[index] = Integer.valueOf(temp);

            System.out.print("ISBN :");
            isbn[index] = keyboard.nextLine();
            totalbooks++; // adds +1 to the total number of books after a new entry

    } // ends data entry loop



    // Prints formatted column names and underlining
            System.out.println(String.format("%-20s", "Title")+
                    String.format("%-20s", "Author")+
                    String.format("%-20s", "Publisher")+
                    String.format("%-20s", "Price")+
                    String.format("%-20s", "Pages")+
                    String.format("%-20s", "ISBN"));
            System.out.println(String.format("%-20s", "====")+
                    String.format("%-20s", "=====")+
                    String.format("%-20s", "=========")+
                    String.format("%-20s", "=======")+
                    String.format("%-20s","=====")+
                    String.format("%-20s","====="));

    // Loop to display entered information 
            for (index = 0; index <totalbooks; index++)
            { 
                System.out.print(String.format("%-20s", title[index])+
                        String.format("%-20s", author[index])+
                        String.format("%-20s", publisher[index])+
                        "£" + String.format("%-20s",  price[index])+ 
                        String.format("%-20s", pages[index])+
                        String.format("%-20s", isbn[index]));
                System.out.println();// line break

            }

    // line breaks
            System.out.println();
            System.out.println();
            System.out.println();

    // data for totals and averages

            System.out.println("Totals");
            System.out.println("-------------------------------");
            System.out.print("Total number of books: "); 
            System.out.println(totalbooks);
            System.out.print("Total cost of books: £");
            System.out.println(sum);



            double max = price[];

                        for (int counter = 1; counter < price.length; counter++)
                        {
                         if (price[counter] > max)
                         {
                          max = price[counter];
                         }
                        }








            System.out.println("Maximum cost of a book :" + max);

            System.out.println("Minimum cost of a book :");

            System.out.print("Average cost of a book :");
            System.out.println(sum/totalbooks);


}
}

I am trying to output the max price, but it keeps throwing

java.lang.NullPointerException

on the line if (price[counter] > max)

I cant figure this out but its probably very obvious, can anyone help?

Thank you

adamjeff
  • 1
  • 2
  • The heuristic for debugging a NullPointerException is almost always the same: 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 Mar 08 '20 at 12:33
  • Thank you I have tried but I am struggling I have read the post and back through the code and I can't solve the problem that is why I made this post. – adamjeff Mar 08 '20 at 12:52
  • You have not read the other question/answer critically. If you did (and even my comment above), you'd have already posted the stack trace, indicated which line throws the exception, told us which variable is null, why you think it should not be null.... Please improve your question. – Hovercraft Full Of Eels Mar 08 '20 at 12:54
  • Our problem is that as your question is currently written there is no way that any one of us can solve your problem or answer your question. The question is woefully incomplete. – Hovercraft Full Of Eels Mar 08 '20 at 12:54
  • Okay thank you for helping; the stack trace is Exception in thread "main" java.lang.NullPointerException at test.projectone.main(projectone.java:119) which points to the line price[counter] = max the reason I cannot understand this is I think they all have a value I will edit the post to contain the full code thanks again – adamjeff Mar 08 '20 at 12:58
  • Where do you declare and initialize the `price` array? Is it an array of type `int` or of type `Integer`? or of type `double` or `Double`? Please show the relevant code. – Hovercraft Full Of Eels Mar 08 '20 at 13:02
  • You will want to [edit] and (again) improve the question – Hovercraft Full Of Eels Mar 08 '20 at 13:03
  • thank you ive edited, the array is a Double – adamjeff Mar 08 '20 at 13:06
  • ***Don't*** make it an array of `Double`. Use `double` instead. Every array is filled initially with default values when first created (unless you create it with initial values yourself). The default value for `double` is `0.0` while for `Double` is `null` since `Double` is a *reference* type. This is key. – Hovercraft Full Of Eels Mar 08 '20 at 13:10
  • Thank you so much for helping I really appreciate your time. Sorry for my inexperience with this you have been incredibly helpful – adamjeff Mar 08 '20 at 13:12
  • 1
    A side note: if this were my project, I would avoid using parallel arrays as you're doing and instead create a new class, say called Book, give it several fields including a title String field, a publisher String field, a `ArrayList` field for authors,..., and then I'd give the class above an `ArrayList` list field , say called books, and fill it with Book objects. – Hovercraft Full Of Eels Mar 08 '20 at 13:12

0 Answers0