This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 4 years ago.
I am very confused!!
In my Question class, I am trying to print a question in an array and the corresponding multiple choice answers stored in a separate 2D array. I put that in a loop and am passing int row from the main method and trying to get user input and pass that back to my Question class. Probably doesn't make sense but I get a null point error.
Here's all of my code
public class Question{
private int selectedAnswer;
public String[] questions =
{
"Favourite sweet",
"Favourite subject at Hogwarts",
"Dream vacation",
"Favourite Drink",
"Dream House",
"What do you desire the most?",
"Favourite dress robe colour",
"Pick a muggle career",
"Pick a creature"
};
private String[][] options =
{
{"1.Acid Pops","2.Sherbert Lemons","3.Bertie Bott's Every Flavour Beans",
"4.Cake","5.Hagrid's Rock Cakes","6.Chocolate Frogs","7.Ginger Newt",
"8.I hate sweets"},
{"1.Care of Magical Creatures","2.Charms","3.Defense Against the Dark Arts",
"4.Divination","5.Herbology","6.History of Magic","7.Muggle Studies","8.Potions",
"9.Study of Ancient Runes","10.Transfiguration"},
{"1.Anywhere with friends","2.Egypt","3.Hogwarts","4.Museum","5.India","6.Forest",
"7.Can't be bothered with a vacation"},
{"1.Unicorn blood", "2.Pumpkin Juice", "3.Butter beer", "4.Coca-Cola", "5.Tea", "6.Coffee", "7.Brandy"},
{"1.The Burrow", "2.A Cottage", "3.Thirteen Grimmauld Place", "4.Malfoy Manor"},
{"1.Friends", "2.Success", "3.Money", "4.Power"},
{"1.Black", "2.Red", "3.Pink", "4.Green", "5.Orange", "6.Blue"},
{"1.Lawyer", "2.Teacher", "3.Social Worker", "4.Prime Minister", "5.Google Employee"},
{"1.Centaur", "2.Basilisk", "3.Unicorn", "4.Thestral", "5.Phoenix", "6.Hippogriff", "7.Dementor"}
};
private String[] quizQuestion;
private String[][] quizOptions;
//new update:
private String quizQuestion;
private String[] quizOptions
public Question(int row){
for(int i= row; i< questions.length; i++){
**quizQuestion[i] = questions[i];** //null point error
quizQuestion = questions[i]; // new update
for(int j = row; j < options[i].length; j++){
quizOptions[i][j] = options[i][j];
quizOptions[i] = options[i][j]; // new update
}
}
}
public String[] getQuizQuestion(){
**return this.quizQuestion;** //null point error
}
public String[][] getQuizOptions(){
return this.quizOptions;
}
public void setSelectedAnswer(int userInput){
selectedAnswer = userInput;
}
}
Main method
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Question q = new Question(0);
System.out.println(q.getQuizQuestion());
System.out.println(q.getQuizOptions());
Scanner keyboard = new Scanner (System.in);
int userInput = keyboard.nextInt();
System.out.println("Select an answer: ");
q.setSelectedAnswer(userInput);
}
}
You have to create an array before you can fill it.
public Question(int row){
quizQuestion = new String[ <expected length> ];
quizOptions = new String[ <expected length> ][]; //Outer array
for(int i= row; i< questions.length; i++){
**quizQuestion[i] = questions[i];** //null point error
quizOptions[i] = new String[]; //Each inner array
for(int j = row; j < options[i].length; j++){
quizOptions[i][j] = options[i][j];
}
}
}
But, as far as I can tell, quizQuestion should just be one String. And quizOptions should be a String array. If a Question object holds the details of only one question, the question should be a String and the options should be only the options of that question.
Update:
private String quizQuestion;
private String[] quizOptions
public Question(int row){
quizQuestion = questions[row];
quizOptions = new String[options[row].length];
for(int j = 0; j < options[row].length; j++){
quizOptions[j] = options[row][j];
}
}
public String getQuizQuestion(){
return this.quizQuestion;
}
public String[] getQuizOptions(){
return this.quizOptions;
}
Also, you may not need to copy options at all:
private String quizQuestion;
private String[] quizOptions
public Question(int row){
quizQuestion = questions[row];
quizOptions = options[row]; //Just refer to the existing piece
}
public String getQuizQuestion(){
return this.quizQuestion;
}
public String[] getQuizOptions(){
return this.quizOptions;
}
Related
Hey I'm trying to calculate the of a quiz by comparing two arrays. The two arrays set up are the correct answers and student answers of a quiz. I'm trying to get it to look like this:
Student Marks Average
Arnie Score: 4 Percentage: 0.8
This is because Arnie has got 4 questions correct out of 5 questions. The answers of the student and the correct answers are found in the text document which I already loaded onto an array on my code. The text document contains a 2D array with Arnie's answers in the form of chars and a 1D array containing the correct answers (also chars). There are more students but I would like some guidance on how to get the average for Arnie so I can do it for the rest of the names on my own.
Here is my code so far with the two arrays set up.
public class workingCode
{
private static String newline = System.getProperty("line.separator");
public static void stuAnsOut(String arg []) throws IOException
{
File studentAns = new File("Ans_Stu.txt");
Scanner stuAns = new Scanner(studentAns);
String stuAnsString[] = new String[6];
char stuAnsArray[][] = new char[3][5];
String[] stuNameArray = new String[3];
for (int i = 0; i<stuAnsString.length;i++)
{
stuAnsString[i]=stuAns.nextLine();
}
for (int i=0,j=0; i<stuAnsArray.length && j<stuAnsString.length;i++, j=j+2)
{
stuNameArray[i] = stuAnsString[j];
}
for (int i=0,j=1; i<stuAnsArray.length && j<stuAnsString.length;i++, j=j+2)
{
stuAnsArray[i] = stuAnsString[j].toUpperCase().toCharArray();
}
System.out.println("Student Answers: ");
System.out.printf("%5s","Name");
for (int i =0; i<1;i++)
{
for (int j =1; j<(stuAnsArray[i].length+1);j++)
{
System.out.printf("%5s",j);
}
}
System.out.printf("%n");
for (int i =0; i<stuAnsArray.length;i++)
{
System.out.printf("%5s",stuNameArray[i]);
for (int j=0;j<stuAnsArray[i].length;j++)
{
System.out.printf("%5s",stuAnsArray[i][j]);
}
System.out.printf("%n");
}
}
public static void corAnsOut(String arg []) throws IOException
{
File correctAns = new File("Ans_Cor.txt");
Scanner corAns = new Scanner(correctAns);
String corAnsString = corAns.next();
char corAnsArray[] = new char[5];
for (int i=0; i<corAnsArray.length;i++)
{
corAnsArray[i] = corAnsString.toUpperCase().charAt(i);
}
System.out.println("Correct Answers: ");
System.out.println(newline);
for (int i =1; i<(corAnsArray.length+1);i++)
{
System.out.printf("%5s",i);
}
System.out.printf("%n");
for (int i =0; i<corAnsArray.length;i++)
{
System.out.printf("%5s",corAnsArray[i]);
}
System.out.printf("%n");
}
}
I am not allowed to use ArrayLists, only Arrays. And please use the array names I already have in my code. Thanks!
Edit: This is what I got so far. But it is giving me an error:
public static void compareInteger(int corAnsArray [], int stuAnsArray[])
{double score =0;
for (int i = 0; i < stuAnsArray.length; i++)
{if(stuAnsArray[i] == corAnsArray[i])
score += 1.0;
}
System.out.println(score/corAnsArray.length);
}
What i would do is create a class called Student
public class Student {
private string studentName;
private float marks;
private float average;
// Getters & all parameter constructor
}
Then change your working class to
public class workingCode
{
private static String newline = System.getProperty("line.separator");
public static void stuAnsOut(String arg []) throws IOException
{
File studentAns = new File("Ans_Stu.txt");
Scanner stuAns = new Scanner(studentAns);
List<Student> students = new ArrayList<Student>();
while(stuAns.hasNext()) {
string parts = stuAns.nextLine().split(" ");
students.add(new Student(parts[0],parts[1],parts[2]))
}
// At this point you have all the students in the List
}
}
Now to calculate avg :
float total = 0;
for(Student s : students) {
total += s.getMarks();
}
System.out.println("avg = " + total/students.size());
or
float total = 0;
for(Student s : students) {
total += s.getAverage();
}
System.out.println("avg = " + total);
Code for comparing arrays (both for INT and String arrays):
public class Compare{
public static void main(String[] args){
double averageInt;
double averageString;
int[] correctInt = {9,8,12,6,3,12,90}; //Correct answers
int[] answerInt = {9,8,4,6,3,12,90}; //Student's answers
String[] correctString = {"A","A","B","C"}; //Correct answers
String[] answerString = {"A","C","B","C"}; //Student's answers
Compare compare = new Compare();
averageInt = compare.compareInteger(correctInt, answerInt);
averageString = compare.compareString(correctString, answerString);
System.out.println(averageInt);
System.out.println(averageString);
}
public double compareInteger(int[] correct, int[] answer){ //Compares the int arrays
double score = 0;
for(int i = 0; i < correct.length; i++){
if(correct[i] == answer[i])
score += 1.0;
}
return score/correct.length; //Returns average
}
public double compareString(String[] correct, String[] answer){ //Compares the String arrays
double score = 0;
for(int i = 0; i < correct.length; i++){
if(correct[i].equals(answer[i]))
score += 1.0;
}
return score/correct.length; //Returns average
}
}
I do not understand the marking system though. If you'd explain it more in-depth, I'd be more than happy to provide you with the code.
I want to create a game and I need to read file from the notepad
when I use my loadfile.java alone, it work very well. Then, I would like to copy my data into datafile.java as it will be easier for me to do the fighting scene. However, I can't copy the array in my loadfile.java to the datafile.java and I don't understand why.
import javax.swing.*;
import java.io.*;
import java.util.Scanner;
public class loadfile
{
static String filename = "Save.txt";
static int size = 4;
static int s;
static int[] number;
static String[] line;
private static void load() throws IOException
{
BufferedReader reader = new BufferedReader(new FileReader(filename));
while (reader.readLine()!= null)
{
size++;
}
size -= 4;
reader.close();
line = new String[size];
number = new int[size];
BufferedReader reader2 = new BufferedReader(new FileReader(filename));
for (int i = 0; i < size; i++)
{
line[i] = reader2.readLine();
}
reader2.close();
for (int i = 4; i < size; i++)
{
number[i] = Integer.parseInt(line[i]);
}
}
public static String[] getData()
{
return line;
}
public static int[] getNumber()
{
s = size - 4;
int[] num = new int[s];
for (int i = 0; i < s; i++)
{
num[i] = number[i+4];
}
return num;
}
public static int getDataSize()
{
return size;
}
public static int getNumberSize()
{
return size - 4;
}
This is my loadfile.java
I use the file with 4 names and 9 * n int in the notepad as I want to check whether I have the character first before I read the file. However, before I can handle this problem, I got another problem that I can't copy the array into my datafile.java
The datafile.java is separate with two constructor. One is for Starting the game and one is for loading the data. The constructor with the (int num) is the problem I have. First, I would like to show the java first:
import java.util.Arrays;
import java.io.*;
public class datafile
{
private static String[] data;
private static int[] number;
private static String[] name;
private static int[] a, d, s;
private static int[] hp, maxhp;
private static int[] mp, maxmp;
private static int[] lv, exp;
public datafile()
{
initialization();
name[0] = "Pet";
a[0] = 100;
d[0] = 100;
s[0] = 100;
hp[0] = 500;
mp[0] = 500;
maxhp[0] = 500;
maxmp[0] = 500;
exp[0] = 100;
lv[0] = 1;
}
public datafile(int num) throws IOException
{
initialization();
loadfile l = new loadfile();
for (int i = 0; i < l.getNumberSize(); i++)
{
number[i] = l.getNumber()[i];
}
for (int i = 0; i < l.getDataSize(); i++)
{
data[i] = l.getData()[i];
}
for(int i = 0; i < 4; i++)
{
name[i] = data[i];
}
for(int i = 0; i < 4; i++)
{
a[i] = number[1+(i*9)];
d[i] = number[2+(i*9)];
s[i] = number[3+(i*9)];
hp[i] = number[4+(i*9)];
mp[i] = number[5+(i*9)];
maxhp[i] = number[6+(i*9)];
maxmp[i] = number[7+(i*9)];
lv[i] = number[8+(i*9)];
exp[i] = number[9+(i*9)];
}
}
public static String getName(int n)
{
return name[n];
}
public static int getAttack(int n)
{
return a[n];
}
public static int getDefense(int n)
{
return d[n];
}
public void initialization()
{
name = new String[3];
a = new int[3];
d = new int[3];
s = new int[3];
hp = new int[3];
mp = new int[3];
maxhp = new int[3];
maxmp = new int[3];
lv = new int[3];
exp = new int[3];
}
public static void main (String[] args) throws IOException
{
new datafile(1);
}
}
When I run the program, the debugging state this line
data[i] = l.getData()[i];
as an error
I don't know what wrong with this line and I tried so many different ways to change the way the copy the method. However, it didn't work
The error says this:
Exception in thread "main" java.lang.NullPointerException
at datafile.<init>(datafile.java:38)
at datafile.main(datafile.java:92)
I hope you guys can help me with this problem because I don't want to fail with my first work
in your datafile(int num)
you call
loadfile l = new loadfile();
but you never call the load() method on you loadfile
l.load();
Edit: my bad, I didn't see your initialization method, but regardless, I'm going to stick with my recommendation that you radically change your program design. Your code consists of a kludge -- you've got many strangely named static array variables as some kind of data repository, and this suggests that injecting a little object-oriented design could go a long way towards creating classes that are much easier to debug, maintain and enhance:
First I recommend that you get rid of all of the parallel arrays and instead create a class, or likely classes, to hold the fields that need to be bound together and create an ArrayList of items of this class.
For example
public class Creature {
private String name;
private int attack;
private int defense;
// constructors here
// getters and setters...
}
And elsewhere:
private List<Creature> creatureList = new ArrayList<>();
Note that the Creature class, the repository for some of your data, should not be calling or even have knowledge of the code that loads the data, but rather it should be the other way around. The class that loads data should create MyData objects that can then be placed within the myDataList ArrayList via its add(...) method.
As a side recommendation, to help us now and to help yourself in the future, please edit your code and change your variable names to conform with Java naming conventions: class names all start with an upper-case letter and method/variable names with a lower-case letter.
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 8 years ago.
I would like to ask help for my project. I am a newbie programmer and I don't get this error "exception in thread main java.lang.nullpointerexception"
this is my code:
public class Slumbook{
public String codeName;
public Slumbook(){
//
}
public Slumbook(String codeName){
this.codeName = codeName;
}
public String getCodeName(){return codeName;}
public void setCodeName(String codeName){this.codeName = codeName;}
}
And the Driver Program code is:-
import java.util.*;
public class SlumbookD{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
Slumbook[] slum = new Slumbook[20];
for(int n=0; n<10; n++){
slum[n].setCodeName(sc.nextLine());
}
}
}
You allocated the array ... but you failed to allocate any objects contained in the array.
SUGGESTION:
import java.util.*;
public class SlumbookD {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
Slumbook[] slum = new Slumbook[20];
for(int n=0; n<slum.length; n++){
slum[n] = new Slumbook(sc.nextLine);
}
}
}
... OR BETTER ...
// You only need one class - with it's own main(). Not two classes...
public class Slumbook{
public String codeName;
public Slumbook(String codeName){
this.codeName = codeName;
}
public String getCodeName(){
return codeName;
}
public void setCodeName(String codeName){
this.codeName = codeName;
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
Slumbook[] slum = new Slumbook[20];
for(int n=0; n<slum.length; n++){
slum[n] = new Slumbook(sc.nextLine);
}
}
}
You have created blank array but not initialize it, you have to associate object reference at your array index and then you can call any number of setter methods on that index.
Slumbook[] slum = new Slumbook[20];
for(int n=0; n<10; n++){
slum[n] = new Slumbook(); // Instantiate Slumbook class and assign reference
slum[n].setCodeName(sc.nextLine());
}
You need to initialize the Array elements before using them.
Slumbook[] slum = new Slumbook[20];
for(int n=0; n<10; n++){
slum[n] = new Slumbook();
slum[n].setCodeName(sc.nextLine());
}
You can initialize all the elements of array at once to avoid any future Exceptions
Slumbook[] slum = new Slumbook[20];
for(int i = 0 ; i < slum.length ; i++) //initializing all the elements
slum[i] = new Slumbook();
for(int n=0; n<10; n++){
slum[n].setCodeName(sc.nextLine());
}
I discovered what i think is a bug whilst using netbeans. When i call up my method to sort an array containing names(ob.sort) in alphabetical order it automatically sorts another array which contains the original names when it isn't supposed to as the original names is not assigned to anything after it has been populated with input at the beginning(ob.input).
I experienced this problem whilst writing larger programs(encountered more than once), but i made a simpler one to demonstrate this problem. It looks like much as i copied the class methods an pasted it below the main class making it easier for you to trace the variables in the program.
public static void main(String args[]){
ObjectTest ob = new ObjectTest();
ob.input();
String x[] = ob.getNames();
System.out.println(x[0]);
ob = new ObjectTest(x);
System.out.println(x[0]);
ob.sort();
System.out.println(x[0]);
String y[] = ob.getNamesrt();
System.out.println(x[0]);
}
}
/*import java.io.*;
import javax.swing.*;
public class ObjectTest {
String name[];
String namesrt[];
public ObjectTest(){
name = new String[3];
namesrt = new String[3];
}
public ObjectTest(String j[]){
namesrt = j;
}
public void input(){
for(int i = 0; i < name.length; i++){
name[i] = JOptionPane.showInputDialog("Enter name");
}
}
public void sort(){
if(!(namesrt == null)){
for(int i = 0; i < namesrt.length; i++){
for(int c = i + 1; c < namesrt.length; c++){
if(namesrt[i].compareToIgnoreCase(namesrt[c]) > 0){
String n = namesrt[i];
namesrt[i] = namesrt[c];
namesrt[c] = n;
}
}
}
}
else{JOptionPane.showMessageDialog(null,"Names not received");}
}
public String[] getNames(){
return name;
}
public String[] getNamesrt(){
return namesrt;
}
public void setNames(String j[]){
name = j;
}
public void setNamesrt(String j[]){
namesrt = j;
}
}*/
I discovered what i think is a bug whilst using netbeans.
Well, it may be a bug in your code. It's not a bug in Java or in Netbeans. It's just demonstrating the fact that arrays are reference types in Java, and the way that objects work.
Here's a short but complete program demonstrating the same effect:
public class Test {
public static void main(String[] args) {
String[] x = { "hello" };
// Copy the *reference*
String[] y = x;
System.out.println(y[0]); // Prints "hello"
x[0] = "new value";
System.out.println(y[0]); // Prints "new value"
}
}
The values of x and y here are references to the same array object... so if the array is changed "through" x, that change is still visible as y[0].
If you want to make your code create independent objects, you'll want to change this:
public ObjectTest(String j[]){
namesrt = j;
}
to:
public ObjectTest(String j[]){
namesrt = j.clone();
}
(Ideally change it to declare the parameter as String[] j, or better yet fix all your variable names to be more meaningful, but that's a different matter.)
Following is the question which I am trying to solve-
In a one day international, the bowling figures of all the bowlers have been provided. The objective is to create an array of Bowler and return it. Note that the objects should appear in the same order in the array as they appear in the input.
The input is provided a string. The string has space demarcated details of each bowler provided as Name-Overs-Maiden-Runs-Wickets, like , “Zaheer-10-1-55-0 Harbhajan-8.4-0-44-2 Ishant-10-0-71-1″.Define a function that takes and prints the array of Bowler returned.
There is some error in the code but I am unable to detect it.
public class MakeArrayOfBowlers{
String name;
double over;
int maiden;
int runs;
int wickets;
public MakeArrayOfBowlers(String input){
String[] str=input.split("-");
this.name=str[0];
this.over=Double.parseDouble(str[1]);
this.maiden=Integer.parseInt(str[2]);
this.runs=Integer.parseInt(str[3]);
this.wickets=Integer.parseInt(str[4]);
}
public MakeArrayOfBowlers[] makeBowlers (String input){
MakeArrayOfBowlers str= (MakeArrayOfBowlers) new MakeArrayOfBowlers("Zaheer-10-1-55-0 Harbhajan-8.4-0-44-2 Ishant-10-0-71-1");
String[] str1 = input.split(" ");
MakeArrayOfBowlers bowler[]= new MakeArrayOfBowlers[str1.length];
for(int i = 0; i < str1.length; i++){
bowler = new MakeArrayOfBowlers[str1.length];
MakeArrayOfBowlers obj = new MakeArrayOfBowlers(str1[i]);
bowler[i] = obj;
}
return bowler;
}
}
You should make a own class bowler (constructor should be better but its your example ;)):
public class Bowler {
private String name;
private double over;
private int maiden;
private int runs;
private int wickets;
public Bowler(String input){
String[] str=input.split("-");
this.name=str[0];
this.over=Double.parseDouble(str[1]);
this.maiden=Integer.parseInt(str[2]);
this.runs=Integer.parseInt(str[3]);
this.wickets=Integer.parseInt(str[4]);
}
public String getName() {
return name;
}
public double getOver() {
return over;
}
public int getMaiden() {
return maiden;
}
public int getRuns() {
return runs;
}
public int getWickets() {
return wickets;
}
}
Than split your string and add it for every bowler:
public class MakeArrayOfBowlers {
public static Bowler[] makeBowlers(String input) {
String[] splitArray = input.split(" ");
Bowler[] bowler = new Bowler[splitArray.length];
for (int i = 0; i < splitArray.length; i++) {
bowler[i] = new Bowler(splitArray[i]);
}
return bowler;
}
public static void main(String[] args) {
Bowler[] bowlers = makeBowlers("Zaheer-10-1-55-0 Harbhajan-8.4-0-44-2 Ishant-10-0-71-1");
for (Bowler bowler : bowlers) {
System.out.println(bowler.getName()+"-"+bowler.getOver()+"-"+bowler.getMaiden()+"-"+bowler.getRuns()+"-"+bowler.getWickets());
}
}
}
You're re-initializing the bowler array inside the for-loop. Try removing that line (the first line inside your loop).
public MakeArrayOfBowlers[] makeBowlers (String input){
String[] str1 = input.split(" ");
MakeArrayOfBowlers bowlers[]= new MakeArrayOfBowlers[str1.length];
for(int i = 0; i < str1.length; i++){
MakeArrayOfBowler bowler = new MakeArrayOfBowlers(str1[i]);
bowlers[i] = obj;
}
return bowlers;
}
From your main function call
MakeArrayOfBowlers o = new MakeArrayOfBowlers();
MakeArrayOfBowlers[] b = o.makeBowlers("Zaheer-10-1-55-0 Harbhajan-8.4-0-44-2 Ishant-10-0-71-1");