Why does this array initializer throw a NullPointerException? [closed] - java

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am studying Java currently, and I am wondering why this code throws a NullPointerException at the line indicated below. The Question object is a user-defined class that takes two strings as parameters when initializing the object.
public class QuizTime
{
public static void main (String[] args)
{
Quiz qz = new Quiz();
// Throws a NullPointerException
qz.add (new Question ("How may US states are there?", "50"));
}
}
Below is the supporting class. The NullPointerException also indicates a problem with the line "quiz[count] = q;"
import java.util.Scanner;
public class Quiz
{
private Question[] quiz;
private int count;
private final int MAX_QUESTIONS = 25;
public void Quiz ()
{
quiz = new Question[MAX_QUESTIONS];
count = 0;
}
public void add (Question q)
{
if (count < MAX_QUESTIONS)
{
// Throws a NullPointerException
quiz[count] = q;
count++;
}
}

public void Quiz ()
should be
public Quiz()
in order to be considered a constructor and correctly initialize your object. Otherwise, it is a method which you haven't invoked.

Related

Why does terminal tell me to change main to string [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
This is the program I'm trying to run
public class PracticeTwo {
public static void main(int[] args){
int[] lower = {-4,-3,-7};
for(int i=0; i<0; i++){
int[] greater = {5,2,6};
for(int j=0; j>0; j++){
if(greater.length>0);
System.out.println(Integer.toString(j) + " is greater than 0");
if(lower.length<0);
System.out.println(Integer.toString(i) + " is lower than 0");
}
}
}
}
There is no compiling issues but when i try to run it returns this:
Error: Main method not found in class PracticeTwo, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
The problem is in the declaration of your main method, as the error description reads, it should be:
public static void main(String[] args)
Pay close attention to the errors and warnings, they exist for a reason.
This is how it should be listed:
public static void main(String[] args){
System.out.println("Hello World!");
}
Anything inside this method will run when you run the program.

this code of mine has no error but it isnt showing any output [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
In this code the method setEid isn't working dont know what's the problem with while loop
this code of mine has no error but it isnt showing any output
public class Emp {
private String eid;
public String getEid() {
return eid;
}
public void setEid(String e) {
while (e.length() < 12) {
eid = e;
}
}
}
public class Test {
public static void main(String[] args) {
Emp z = new Emp();
z.setEid("rgrge");
System.out.println("\n" + z.getEid());
}
}
enter code here i expect the static initialization of setEid argument should not take more than 12 characters
Your setter method have a while loop if eid length is less than 12 then it always stuck in this method.
public void setEid(String e){
while(e.length()<12)
eid=e;
}
and In your main method
public static void main(String[] args) {
Emp z=new Emp();
z.setEid("rgrge"); // you call setter
System.out.println("\n"+z.getEid());
}
you pass "rgrge" in setter and its length is less than 12. Tht's why your program is stuck in loop and not showing any thing.
Change setter implementation to this.
public void setEid(String e){
if(e.length()<12) // change while to if
eid=e;
}
so first there should have had if instead of while.
Change:
while(e.length()<12) change this as if(e.length()<12)
Explanation:
your setEid method never going to run because there is no break condition there. your while loop condition tends to the infinite. your code gets stuck in that while loop.
here you passed z.setEid("rgrge");. in your example, Length of string e is 5 which is obviously less than 12, so the condition will always be satisfied and your program will be stuck in infinite loop.

Why call to " this " must be first statement in constructor? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I am trying to execute this below code sample to understand why call to " this " must be first statement in constructor ?? I had read lot of About it and I understand Why this is so !!
so I write the below simple program But Still showing me the same Error even I use 'this' as a First Statement in my Program .
import java.io.*;
import java.lang.*;
class Demo
{
int x=23;
Demo()
{
this(55);
}
Demo(int x)
{
this.x=x;
System.out.println("Inside Parameterise Constructor 2"+"\n Value of x:"+x);
}
}
class ThisDemo
{
public static void main(String []args)
{
Demo obj = new Demo();
}
}
To specifically answer your question, this or super needs to be the first call to ensure the base class has been setup correctly.
https://stackoverflow.com/a/1168356/154186
To solve your error above, remove the void type from the function call. e.g.:
Demo(int x) {
this.x = x;
}
Demo() {
this(50);
}
Remove void from Demo constructors
class Demo
{
int x=23;
Demo()
{
this(55);
}
Demo(int x)
{
this.x=x;
System.out.println("Inside Parameterise Constructor 2"+"\n Value of x:"+x);
}
}
You should remove void.Constructor must have no explicit return type.
Then it will work fine.

Vector Method Java [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I am trying to create a vector method called readfromfile which would potentially read the input from a different text file. Why does it give an error?
Edit: Thanks for the help, I have edited the code and it works!
Looks like I was confusing parameters and methods! :P
Thanks guys :D
package cas.lab1.firsteclipsePackage;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.Vector;
public class FirstEclipseClass {
public static void main(String[] args) {
Vector input = new Vector();
input.add("A");
input.add("B");
input.add("C");
input.add("D");
printVectorElements(input, 3);
Vector<String> results = readFromFile();
}
public static void printVectorElements(Vector input, int count) {
for (int i = 0; i < count; i++) {
System.out.println(input.get(i));
}
}
public static Vector<String> readFromFile(){ //yeah I did confuse methods and parameters
Vector<String> result = new Vector<String>();
try{
File f = new File("input.txt");
Scanner s = new Scanner(f);
while(s.hasNextLine()) {
int i = s.nextInt();
if(i % 2 == 0)
result.add("Even");
else
result.add("Odd");
System.out.println(i);
}
s.close();
}
catch(FileNotFoundException e){
e.printStackTrace();
}
return result;
}
}
I guess that you are confused here. From your method call, I see that you don't need to pass any parameters, and instead want a Vector back. So I suggest you to change this line:
public static readFromFile(Vector<String> results){
To this line:
public static Vector<String> readFromFile(){
First thing: you didn't specified the return type. You should have :
public static Vector<String> readFromFile()
if you do not need any parameters in the function.
Second, for future, you cannot have this same name in the function and as a function parameter

Why is it showing ";" expected in main statement while compiling? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Not able to compile and it is showing ; expected at the end of public static statement.
//Recursive program
class Recur
{
public static void main(String args[])
void witty(String n, int p)
{
if(p<0)
System.out.println("");
else
{
System.out.println(n.charAt(p)+".");
witty(n, p-1);
System.out.print(n.charAt(p));
}
}
}
It seems that you forgot the body of the main method:
public static void main(String args[]) {
}
Note that the body of a method is delimited by { and }.

Categories