Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i am writing a program to implement "quiz" in java8.
fIRST i would explain my Quiz.
In quiz class I have to add questions to a arrayList and display them.
In question class u have to set a question and its 4 options.
here i am getting null pointer exception while compilation. You would write your questions to an ArrayList and display them in second method in quiz class.
In menu if you choose 1 you get add questions or type 2 you could get list of questions.I am still working on printing questions.

What I have tried:

Java
import java.util.Scanner;
import java.util.*;

class Question{
	String question ;
	int no;
	HashMap<integer,string> options;
	String correctanswer;

	public static void  q_no(){
		System.out.println("enter a question no");
		int num;
		Scanner no = new  Scanner(System.in);
		num = no.nextInt();
	}
	public static void set_question(){
		System.out.println("enter aquestion");
		Scanner f = new Scanner(System.in);
		String question = f.nextLine();

	}
	public  void set_options(){
		System.out.println("enter options");
		int count =1;
		String q;
		Scanner str = new Scanner(System.in);
		while(count<5){
			System.out.print("set option "+count);
			q= str.nextLine();
			options.put(count,q);
			count++;
		}
	}
	public  void set_correctanswer(){
		System.out.println("enter correct answer");
		Scanner d = new Scanner(System.in);
		String ans = d.nextLine();


	}

	public static void marks(){

	}

               

}
class Quiz{
	ArrayList<question> list = new ArrayList<question>();
	public ArrayList<question> adding(Question q){
		list.add(q);
		return list;
	}
	public void show(){
		int g = list.size();
		int count =0;
		while(count<g){>
			list.get(count);
		}
	}
}

class Menu{
	public static void main(String[] args) {
		System.out.println("enter 1 for adding questions");
		Scanner input = new Scanner(System.in);
		int g;
		g= input.nextInt();
		if (g == 1){
			System.out.println("how many questions do you add");
			int number;
			Scanner b = new Scanner(System.in);
			Quiz programming = new Quiz();
			number = b.nextInt();
			int count=0;
			while(count<number){>
				Question q1 = new Question();
				q1.q_no();
				q1.set_question();
				q1.set_options();
				q1.set_correctanswer();
				programming.adding(q1);

			}
			


		}
		else if(g ==2){
			Quiz programming = new Quiz();
			programming.show();
			
		}

	}
}</question></question></question>
Posted
Updated 24-Mar-18 10:58am
v2
Comments
Patrice T 24-Aug-16 12:26pm    
And the compiler didn't told you where is this error ?

This is one of the most common problems we get asked, and it's also the one we are least equipped to answer, but you are most equipped to answer yourself.

Let me just explain what the error means: You have tried to use a variable, property, or a method return value but it contains null - which means that there is no instance of a class in the variable.
It's a bit like a pocket: you have a pocket in your shirt, which you use to hold a pen. If you reach into the pocket and find there isn't a pen there, you can't sign your name on a piece of paper - and you will get very funny looks if you try! The empty pocket is giving you a null value (no pen here!) so you can't do anything that you would normally do once you retrieved your pen. Why is it empty? That's the question - it may be that you forgot to pick up your pen when you left the house this morning, or possibly you left the pen in the pocket of yesterdays shirt when you took it off last night.

We can't tell, because we weren't there, and even more importantly, we can't even see your shirt, much less what is in the pocket!

Back to computers, and you have done the same thing, somehow - and we can't see your code, much less run it and find out what contains null when it shouldn't.
But you can - and Visual Studio will help you here. Run your program in the debugger and when it fails, VS will show you the line it found the problem on. You can then start looking at the various parts of it to see what value is null and start looking back through your code to find out why. So put a breakpoint at the beginning of the method containing the error line, and run your program from the start again. This time, VS will stop before the error, and let you examine what is going on by stepping through the code looking at your values.

But we can't do that - we don't have your code, we don't know how to use it if we did have it, we don't have your data. So try it - and see how much information you can find out!
 
Share this answer
 
One obvious problem is that your q_no method (not sure what it is supposed to do) and set_question have been declared static so do not have access to the fields in your Question class. You should change it to:
Java
public void set_question(){
    System.out.println("enter aquestion");
    Scanner f = new Scanner(System.in);
    question = f.nextLine(); // this will now set the instance variable to the string
    
}
 
Share this answer
 
Comments
Papasani Mohansrinivas 24-Aug-16 12:54pm    
thank you but i keep getting error at method set_options not set_questions
Richard MacCutchan 24-Aug-16 13:10pm    
What error? Please do not expect us to guess what you see on your screen.
Papasani Mohansrinivas 24-Aug-16 12:55pm    
could you please provide solution to above comment
I fixed the syntax errors (you know Java is case sensitive, don't you?).
I fixed some runtime errors as well. Still the program is incomplete.
Java
import java.util.Scanner;
import java.util.*;

class Question
{
  String question ;
  int no;
  HashMap<Integer,String> options;
  String correctanswer;

  public static void  q_no(){
    System.out.println("enter a question no");
    int num;
    Scanner no = new  Scanner(System.in);
    num = no.nextInt();
  }
  public static void set_question(){
    System.out.println("enter aquestion");
    Scanner f = new Scanner(System.in);
    String question = f.nextLine();

  }
  public  void set_options(){
    System.out.println("enter options");
    int count = 1;
    options = new HashMap<Integer, String>();
    String q;
    Scanner str = new Scanner(System.in);
    while(count<5){
      System.out.print("set option "+count);
      q= str.nextLine();
      options.put(count,q);
      count++;
    }
  }
  public  void set_correctanswer(){
    System.out.println("enter correct answer");
    Scanner d = new Scanner(System.in);
    String ans = d.nextLine();
  }

  public static void marks(){
  }
}
class Quiz
{
  ArrayList<Question> list = new ArrayList<Question>();
  public ArrayList<Question> adding(Question q){
    list.add(q);
    return list;
  }
  public void show(){
    int g = list.size();
    int count =0;

    //while(count<g){>
    //  list.get(count);
    //}
  }
}

class Menu{
  public static void main(String[] args) {
    System.out.println("enter 1 for adding questions");
    Scanner input = new Scanner(System.in);
    int g;
    g= input.nextInt();
    if (g == 1){
      System.out.println("how many questions do you add");
      int number;
      Scanner b = new Scanner(System.in);
      Quiz programming = new Quiz();
      number = b.nextInt();
      int count=0;
      while(count<number){//>
        Question q1 = new Question();
        q1.q_no();
        q1.set_question();
        q1.set_options();
        q1.set_correctanswer();
        programming.adding(q1);
        ++count;
      }
    }
    else if(g ==2){
      Quiz programming = new Quiz();
      programming.show();
    }
  }
}
 
Share this answer
 
A SIMPLE PINBALL GAME


1 import javax.swing.*;
2 import java.awt.*;
3 import java.awt.event.*;
4

5 public class Panel00 extends JPanel {
6 Timer t = new Timer(1, new Listener());
7 int ctr = 0;
8 double G = 0.1; //Gravitational Constant
9

10 final int xpos = 280;
11
12 double[] p2d = {280, 200};
13 double[] v2d = {0, 0};
14
15 int points = 0;
16 int lives = 0;
17 int sides = 13;
18
19 double snorm = 400;
20 double sd = 450;
21 double sv = 0;
22 boolean setlock = false;
23 boolean rdown, ldown;
24 double paddle = 130;
25 double rtheta = 0;
26 double ltheta = 0;
27
28 int preset[][] = {
29 {0, 400, 135, 450,1}, //right paddle
30 {135, 450, 270, 400,1}, //left paddle
31 {270, 0, 300, 20, 1}, //first bouncey thingy
32 {291, 0, 291, 500, 1}, //right wall
33 {-1, 0, 270, 0, 1}, //top wall
34 {0, -1, 0, 500, 1} //left wall
35 };
36

37 int[][] balls = {
38 {80, 80, 30, 50},
39 {230, 280, 20, 200},
40 {50, 200, 25, 100},
41 {200, 100, 10, 500}
42 };
43 int lines[][] = new int[100][5];
44
45 public Panel00(){
46 super();
47 t.start();
48 addKeyListener(new Key());
49 setFocusable(true);
50
51 for(int i = 0; i < preset.length; i++){
52 lines[i] = preset[i];
53 }
54
55 int plen = preset.length;
56
57 int ct = 0;
58 for(int k = 0; k < balls.length; k++){
59 int px = balls[k][0], py = balls[k][1], radius = balls[k][2];
60 for(double i = 0; i < 2 * Math.PI; i+= 2 * Math.PI/ sides){
61 ct++;
62 lines[plen + ct][0] = px + (int) (radius * Math.cos(i));
63 lines[plen + ct][1] = py + (int) (radius * Math.sin(i));
64 lines[plen + ct][2] = px + (int) (radius * Math.cos(i - 2 *Math.PI / sides));
65 lines[plen + ct][3] = py + (int) (radius * Math.sin(i - 2 * Math.PI / sides));
66 }
67 }
68
69 }
70
71 private class Listener implements ActionListener {
72 public void actionPerformed(ActionEvent e){
73 repaint();
74 }
75 }
76
77 public void paintComponent(Graphics g){
78 super.paintComponent(g);
79 v2d[1] += G;
80 p2d[1] += v2d[1];
81 p2d[0] += v2d[0];
82
83

84
85 if(p2d[1] > 1000){
86 p2d[0] = 280;
87 p2d[1] = 200;
88 v2d[0] = 0;
89 v2d[1] = 0;
90 lives++;
91 }
92 if(p2d[0] == 280 && p2d[1] > sd){
93 p2d[1] = sd;
94 v2d[1] = Math.min(v2d[1], sv);
95 }
96
97 if(setlock == false){
98 sv *= 0.95; //the dampening coefficient for the springiness
99 sv -= (sd - snorm)/30;
100 sd += sv;
101 }
102 double rc = 0.1;
103 if(rdown){
104 rtheta = Math.max(-0.5, rtheta - rc);
105 }else{
106 rtheta = Math.min(0.5, rtheta + rc);
107 }
108 if(ldown){
109 ltheta = Math.max(-0.5, ltheta - rc);
110 }else{
111 ltheta = Math.min(0.5, ltheta + rc);
112 }
113
114 lines[0][2] = lines[0][0] + (int) (Math.cos(ltheta) * paddle);
115 lines[0][3] = lines[0][1] + (int) (Math.sin(ltheta) * paddle);
116 lines[1][0] = lines[1][2] + (int) (-Math.cos(rtheta) * paddle);
117 lines[1][1] = lines[1][3] + (int) (Math.sin(rtheta) * paddle);
118 int rX = (int) p2d[0];
119 int rY = (int) p2d[1];
120 int r = 10;
121 g.setColor(Color.blue);
122 g.drawArc(rX - r, rY - r, 2 * r, 2 * r, 0, 360);
123 g.setColor(Color.black);
124 for(int i = 0; i < lines.length; i++){
125 int x1 = lines[i][0],
126 y1 = lines[i][1],
127 x2 = lines[i][2];
128 double y2 = lines[i][3] + 0.0001;
129 if(i > preset.length){
130 g.setColor(Color.red);
131 }
132 g.drawLine(x1, y1, x2, (int) Math.round(y2));
133

134 double bmag = Math.sqrt(v2d[0] * v2d[0] + v2d[1] * v2d[1]);
135 double lineslope = ((double)(x2 - x1))/((double)(y2 - y1));
136 double ballslope = v2d[0] / v2d[1];
137 //System.out.println(ballslope + " " + lineslope);
138 //xpos * ballslope + p2d[1] = xpos * lineslope + y1;
139 double binter = p2d[0] - ballslope * p2d[1];
140 double linter = x1 - lineslope * y1;
141
142 double y = (binter - linter)/(lineslope - ballslope);
143 double sx = y * ballslope + binter;
144 //double qx = y * lineslope + linter;
145 double la = Math.atan2(y2 - y1, x2 - x1);
146 double ba = Math.atan2(v2d[1], v2d[0]);
147
148 double da = 2 * la - ba;
149
150 //System.out.println(sx + " " + y);
151 /*
152 g.drawLine((int)sx, (int)y, (int)p2d[0], (int)p2d[1]);
153 g.fillRect((int)sx - 2, (int)y - 2, 4, 4);
154 g.drawLine((int)p2d[0], (int)p2d[1], (int) (p2d[0] + Math.cos(da) * 100), (int)(p2d[1] + Math.sin(da) * 100));
155 //*/
156 if(sx >= Math.min(x2, x1) && sx <= Math.max(x1, x2) &&
157 Math.min(y1, y2) <= y && Math.max(y1, y2) >= y){
158 double interdist = Math.sqrt(Math.pow(sx - p2d[0],2) + Math.pow(y - p2d[1],2));
159 double tiny = 0.0001;
160 double futuredist = Math.sqrt(Math.pow(sx - (p2d[0] + Math.cos(ba) * tiny),2) + Math.pow(y - (p2d[1] + Math.sin(ba) * tiny),2));
161
162 if(interdist <= bmag + r && futuredist < interdist){
163 //System.out.println("Carl Sagan" + i); //this is a pun because he wrote a book called Contact
164 if(i > preset.length){
165 int ball = (int) Math.floor((i - preset.length)/sides);
166 //System.out.println(balls[ball][2]);
167 points += balls[ball][3] * bmag;
168 }
169 v2d[0] = Math.cos(da) * bmag;
170 v2d[1] = Math.sin(da) * bmag;
171 }
172 }
173 }
174 g.setColor(Color.black);
175 //System.out.println(sx + " " + qx);
176 //System.out.println(ballslope + " " + lineslope);
177 //double slope = Math.atan2(v2d[1], v2d[0]);
178 //g.drawLine((int) p2d[0], (int) p2d[1], (int) (p2d[0]+10*v2d[0]), (int) (p2d[1]+10*v2d[1]));
179
180 g.fillRect(xpos - 5, (int)sd + 10, 10, 20);
181
182 g.drawString("Score: " + points + " Resets: " + lives, 10, 15);
183
184 }
185
186 private class Key extends KeyAdapter {
187 public void keyPressed(KeyEvent e){
188 if(e.getKeyCode() == KeyEvent.VK_DOWN){
189 setlock = true;
190 sd += 2;
191 }
192 if(e.getKeyCode() == KeyEvent.VK_LEFT){
193 ldown = true;
194 }
195 if(e.getKeyCode() == KeyEvent.VK_RIGHT){
196 rdown = true;
197 }
198 }
199 public void keyReleased(KeyEvent e){
200 setlock = false;
201 if(e.getKeyCode() == KeyEvent.VK_LEFT){
202 ldown = false;
203 }
204 if(e.getKeyCode() == KeyEvent.VK_RIGHT){
205 rdown = false;
206 }
207 }
208 }
209 }
 
Share this answer
 
Comments
Patrice T 24-Mar-18 17:38pm    
off topic

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900