Getting NullPointer Exception Error In Simple ArrayList Program

Mar 21, 2015

I am creating a simple ArrayList program that would enable one to input their username to it using a scanner. However, i am getting this error: "Exception in thread "main" java.lang.NullPointerException

at home.Members.addUser(Members.java:16)
at home.Main.main(Main.java:14)"

Here is the code! :

Main.java class
Java Code: import java.util.Scanner;
public class Main {

[code]....

View Replies


ADVERTISEMENT

Program Prompt For Input File / Take Its Info And Write To Report - Nullpointer Exception

Apr 17, 2014

I've got a nasty nullpointer that I have tried to resolve to no avail as of yet. The program should prompt for a listings.txt file and take its info and write to a report file. Here's the stacktrace:

run:

Input file: listings
Exception in thread "main" java.lang.NullPointerException
at java.io.Writer.<init>(Writer.java:88)
at java.io.PrintWriter.<init>(PrintWriter.java:113)
at java.io.PrintWriter.<init>(PrintWriter.java:100)
at kettask2b.PropertyListingsReport.main(PropertyListingsReport.java:34)
Java Result: 1

Some adjustments that I have attempted are:

BufferedWriter pwfo = null;
for (int i = 0; i < args.length; i++) {
String string = args[i];
pwfo = null;

[Code] ....

Here's the code:

package kettask2b;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

[Code] ....

View Replies View Related

Exception Errors In Simple Program?

Mar 26, 2015

I'm trying to read lines from a textfile and count all the words using String Tokenizer. However I keep getting an error "Unhandled exception type FileNotFoundException" on my IDE(Eclipse) referring to lines 13 and 8. When I let the IDE automatically, and I've even tried typing this manually, insert a try-catch block more errors show up concerning inputFile. When I insert the throws FileNotFoundException for each method it compiles but after running it gives me a FileNotFoundException for textfile.txt. What's even more interesting is that when I copy the .java file out of the IDE project folder, place it in a random empty folder on the Desktop with the textfile.txt, and try running it through command line, it gives a NoSuchElementException: No line found.

import java.util.Scanner;
import java.util.StringTokenizer;
import java.io.*;

[Code].....

View Replies View Related

NullPointer Exception From Calling A Setter In Array Of Objects

Sep 21, 2014

I hope I'm putting this question in the right folder. I have an array of objects, and I have defined a setter for a variable in the object. When I call the setter, I get a NullPointerException. Here is the relevant code for the object.

public class Digit extends Thread {
private int digit;
public void setDigit(int digit) {
this.digit = digit;
}
// run method follows
}

Here is the portion of the main class where I define an array and then call the setter.

Digit[] digits = new Digit[10];
for (int i = 0; i < digits.length; i++) {
digits[i].setDigit(i); // NullPointerException occurs here
}

View Replies View Related

Write To Text Area Using Button - NullPointer Exception

Apr 25, 2015

I have problem with my simple program. I tried put some text in my text area using button1 but textListener in RighButtons is always null and it's give me NullPointerException.

WriteToArea - interface
public interface WriteToArea {
public void add_a(String text);
}
BaseFrame
public class BaseFrame extends JFrame{

[Code] .....

View Replies View Related

Highlighted Text In Try / Catch Block Is Throwing NullPointer Exception

Sep 15, 2014

If I put the highlighted text in try/catch block it is throwing NullPointerException , if I am using command line arguments then also it is showing the same exception.

public static void main(String []args)
args = null;
args[0] = "test";
System.out.println(args[0]);

View Replies View Related

Getting NullPointer Exception In Unit Test At Activemq Connection Factory Creation

Dec 2, 2014

I have the following unit test that gives me a null pointer exception. The debugger goes to the finally block right after the line that creates a connection factory. Here's the test:

@Test
public void receiveMessage() throws Exception {
MessageConsumer consumer = null;
ConnectionFactory connectionFactory = null;
Connection connection = null;
Session session = null;
Destination destination = null;

[code]....

View Replies View Related

Getting A Nullpointer Exception After Cleaning Up Code By Putting Repetitive Stuff In A Method

May 8, 2014

I am getting a nullpointer exception after "cleaning" up my code by putting repetitive stuff in a method.

The error points to this: ai.getItRight(n, answer);//make sure the user enters yes or no..

The error occurs at lines 19 and 25.Here is the relative code:

public boolean AskQuestions(Node n, String yesOrNo, String answer, String question){
if(yesOrNo.equalsIgnoreCase("no") && n.getRight() == null){//i guessed the wrong answer
System.out.println("I give up. Who is it: ");
answer = input.nextLine();

[code]....

My previous code, which use to have the contents of getItRight in place of lines 19 and 25 worked just fine. So why am I getting this error? I dont want my methods to be crazy big like how they usually end up.

View Replies View Related

Simple Logic Error In Cipher-shifting Program?

Feb 8, 2015

I was tasked with creating a program that encrypts a line of text (for example, CANDY) by shifting the letters X amount of times. For example, if the user inputs the sentence CANDY and selects a shift of 5, the output would be: HFSID. I got this part working fine. The issue I am having is with the decryption part of the program.

This is simply the reverse of the above, as the user would enter the phrase HFSID, with a shift of 5, and the program would output: CANDY. It works fine, all except for one letter, being the "F" letter. With my code, when I enter the above word to be decrypted it outputs: C[NDY

Obviously, that [ bracket is not an 'A'. I realise the issue falls with the equation, Here is my code:

public class SimpleEncryption {

/**
* @param args the command line arguments
*/
static int answer;
public static void main(String[] args) {
String cipher = JOptionPane.showInputDialog(null, "Please enter a sentence or word that you wish to encode or decode. This program uses"
+ " a basic cipher shift.");

[code]....

View Replies View Related

Creating TXT File - Program Keeps Printing Exception Error

Apr 10, 2014

I'm trying to have the program create a .txt file for me but it is not working properly. It just keeps printing the exception error "The file could not be found" and exits the program.

// This program asks the user for input for a name, phone number and notes up to 200 entries.
// It stores every contact in a file. Type 'h' for help while running this program.

import java.util.Scanner;
import java.io.*;
public class Phonebook {
static Entry[] entryList = new Entry[200];
static int numEntries;
public static void main(String[] args) {

[Code] ....

After running the code, I go into my workspace folder and I see that the file was created. What to do because running the program will always print "could not find the file".

View Replies View Related

Magic Square Program - File Not Found Exception Error

Apr 25, 2014

I am working on a magic square program. My program compiles. However, when I enter the square dimension it does not select the correct file. The error says "java.io.FileNotFoundException." It looks like it inserts 0 instead of the entered dimension.

import java.util.*;
import java.io.*;
public class Trial2
{
public static int size, row, col;
public static void main(String[]args)throws java.io.IOException

[Code] ....

View Replies View Related

Concurrent Modification Exception In ArrayList

Mar 27, 2014

My code is like this,

for(Iterator iterator = list.iterator(); iterator.hasNext();) {
String string = iterator.next().toString();
list.add(string + "Hello");
iterator.remove();
}

Its a simple program. I've list with some names as values like,

Ram, Sham, Bham, Bhim, Suraj.

Now, I want to add "Hello" to each name by running above code. While iterating, first time I get Ram, I want to add "Hello" to Ram and add to list as "Ram Hello" also I want to remove original Value i.e "Ram". When I run above code it is giving Concurrent Modification Exception. How to do this?

View Replies View Related

Else Without If Error - Simple Calculator Using JOP

Oct 8, 2014

import javax.swing.JOptionPane;
public class Calculator {
public static void main ( String args[] ) {
double n1 = Double.parseDouble(JOptionPane.showInputDialog(" Enter first number: "));
double n2 = Double.parseDouble(JOptionPane.showInputDialog(" Enter second number: "));
String x = JOptionPane.showInputDialog("Enter operator: ");
double result;

[code].....

Write a program that mimics a calculator. The program will need to accept as input two numeric double values, and accept the operation to be performed as the third input. The operation to be performed will be input in the form of the character representing the java operator for the operation to be performed (i.e + for addition, - for subtraction, * for multiplication, / for division, or % for modulus). Once the program has read these 3 inputs it should output the numbers, the operator, and the result. (For division, if the denominator is zero, the program will output an appropriate message (i.e division by zero is an invalid operation). Please use the JOptionPane class for both accepting the required inputs and for the required output demonstrated below.Some sample output may look as follows:

3 + 4 = 7
13 * 5 = 65
10 / 3 = 3.33333333

Error Messages:
3 errors found:
File: /Users/gcaruso/Documents/CISS 110/Module 3/Module 4/Calculator.java [line: 23]
Error: /Users/gcaruso/Documents/CISS 110/Module 3/Module 4/Calculator.java:23: 'else' without 'if'
File: /Users/gcaruso/Documents/CISS 110/Module 3/Module 4/Calculator.java [line: 31]
Error: /Users/gcaruso/Documents/CISS 110/Module 3/Module 4/Calculator.java:31: 'else' without 'if'
File: /Users/gcaruso/Documents/CISS 110/Module 3/Module 4/Calculator.java [line: 37]
Error: /Users/gcaruso/Documents/CISS 110/Module 3/Module 4/Calculator.java:37: 'else' without 'if

View Replies View Related

How To Avoid ArrayList Concurrent Modification Exception

Apr 7, 2014

I'm writing an IRC server in Java, however when a client tries to connect I get a ConcurrentModificationException trying to add them to an ArrayList. My code is Here: [URL]

I don't understand why I am getting a ConcurrentModificationException or what one is.

View Replies View Related

Running A Simple GUI Program

Nov 23, 2014

I'm getting back into the swing of things with Java after using I'm asked to utilize a simple GUI in order to take in the starting data, I cannot seem to get this to work. I'm getting this error Exception in thread "main" java.lang.NullPointerException

at java.awt.Container.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at Input.buildPanel(Input.java:53)
at Input.<init>(Input.java:27)
at InputDemo.main(InputDemo.java:5)

I've created two classes

import javax.swing.*;
public class Input extends JFrame {
private JPanel panel;
private JLabel messageLabel;
private JLabel messageLabel1;
private JLabel messageLabel2;
private JTextField shiftHrs;

[code]....

View Replies View Related

Simple Three Button Program

Dec 14, 2014

I have my program set up I thought correctly. When I click the buttons, nothing is happening.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class RobertMaloneChapter18 extends JFrame
{
//set final sizes for window
private static final int WIDTH = 300;
private static final int HEIGHT = 200;

[code]....

View Replies View Related

Simple Pyramid Program

Jun 28, 2014

I was learning Java and there was this exersize to construct a simple pyramid of prespecified height and width. The program i wrote is turining up with the wrong result.

package asgn2;
import acm.graphics.*;
import acm.program.*;
public class pyramid extends GraphicsProgram {
private static final int BRICK_WIDTH = 30;
private static final int BRICK_HEIGHT = 12;
private static final int BASE_BRICKS =14;

[code]...

View Replies View Related

Error In Adding In ArrayList

Apr 19, 2014

package eg.edu.guc.iphone.tiles;
import java.util.ArrayList;
import eg.edu.guc.iphone.Location;
public class Square extends Piece {
private ArrayList<Location>possibleMoves;

[Code] ......

View Replies View Related

How To Fix Null Pointer Exception Error

May 16, 2014

what i do keep getting nullpointerexception error whenever i input 1 or 2 whenever the code is asking me to input the type of display to be shown..

this is my error - "Exception in thread "main" java.lang.NullPointerException
at dakilangtaoako.DakilangTaoAko.alphabetSorter(Dakil angTaoAko.java:195)
at dakilangtaoako.DakilangTaoAko.main(DakilangTaoAko. java:91)
Java Result: 1 "
package dakilangtaoako;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;

[code]....

View Replies View Related

Simple Averaging Program Using Two Different Classes?

Apr 9, 2014

This is a simple project that i was using to test my current java knowledge (kinda like revision) and for that i used two classes to make a simple averaging program. i know i0m making it more difficult but like i said i'm testing myself but i come up with an unexpected error. Here's the code:

- 1st Class

import java.util.Scanner;
public class Main {
public static int num;
public static int total = 0;
public static int average;

[Code].....

Now the problem is after inputing the numbers it doesn't give me the average but the value of 0.

View Replies View Related

Simple Encryption / Decryption Program

Nov 1, 2014

I have to write an encryption/decryption program for a sentence entered by the user that uses arrays. Here is my code so far. I'm kind of lost on what to do next in the code.

import java.util.Scanner;

/*Program that encrypts then decrypts a sentence
Encryption
*/
public class Encryption extends java.swing.JFrame{

[code]....

View Replies View Related

Make A Simple Calculator Program

Mar 11, 2015

i am working on my assignment in Compro 2, we are ask to make a simple calculator program

here's my code;

import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JFrame;
import java.awt.BorderLayout;

[code]...

View Replies View Related

How To Make A Simple Connect Four Program Without GUI

Nov 19, 2014

My code below is trying to make a connect four program without GUI. I'm having trouble with getting the players to make their moves. What should I put in my "makeAMove" method...

Java Code:

public class Connect

final static int MAXROW = 6;
final static int MAXCOL = 7;
public static void main(String[] args){

[code]....

View Replies View Related

Simple Vending Machine Program

Jan 22, 2014

I am having a difficult time writing what started out as a simple vending machine program. As I go on though, it seems that I'm overcomplicating it and it is getting messy, so I have resorted to the forum before I really get off the path. how to clean it up? I'm going to list a few of the problems I am having with the code below:

1. The user is supposed to enter his/her money and the program is to read each value separately (Do-While loop) and keep a running total of the money entered. I can't seem to get the right code format to do this. I was trying to do this with the variable total and so that is why the total exists in the switch statement, but it did not work.

2. In the second Do-While statement, is there a way to kick back an error when their are insufficient funds to purchase an item? Instead of getting a negative change return.

package practice;
import java.util.Scanner;
public class Practice {
public static void main(String[] args) {
double count, total;
int item;
//Display Available Options To Customer
Scanner input = new Scanner(System.in);
System.out.println("*VENDING MACHINE*");

[code]....

View Replies View Related

Simple 2D Optics Simulation Program

Sep 13, 2014

I'm very new to Java (I literally started learning it yesterday) and I've been working on a simple program that's meant to simulate interactions between optics objects and light rays.

Everything has been going really well, except that my mirrors only reflect the first ray that comes in contact with them.

Here's a screenshot : 2014-09-13 at 7.39.23 PM.jpg

I wrote 7 classes:

Main class to create the optics objectOptics class. the most important class that uses a recursive function to detect the nearest intersection point of a ray with the nearest optics object. It then calculates a reflected ray and inputs it back into the function until no intersections are found or the max recursion depth is met.Ray. creates a rays (in parametric form O + tD where O is the origin, D is the direction and t is a non-negative scalar)Object. empty abstract class used for polymorphism (so when i add more optics objects like prisms and lenses they'll share the object type)Mirror. Basically a line segment created with the Line classLine. Creates a line segmentDraw. JComponent with paintComponent function that loops through an array of shapes and draws them to the screen.

What I know so far is that the problem boils down to the checkRay() function in the Optics class. When a mirror has already reflected a ray, the line:

Intersection sec = new Intersection(ray,(Mirror)obj);

creates an intersection with a null point.

I debugged it line by line and found the problem was that my variable 't' (that is the parameter for the parametric line which represents the mirror) in the Intersection class got big values when it's meant to be between 0 and 1 (since it's a line segment), which resulted in the function returning null (as it should when 't' is not between 0 and 1).

I've confirmed with tests that this has nothing to do with the specific mirror or angle of incidence. It only occurs when a mirror has already been intersected with by a ray.

I've found out that my function:

public PVector getNormal(PVector D){
D.normalize();
return new PVector(D.y,-D.x);
}

Changes the value of the 'D' PVector of the mirror inside my 'objects' ArrayList. How can it access the private PVector 'D' from outside the Mirror class? This normalization to the direction vector is what causes the Intersection class to return null the second time around!

The problem was that in the getNormal function, the input vector argument was a reference to the 'D' vector for the mirror in my 'objects' ArrayList so the .normalize() function acted upon the original vector, changing it's value and screwing things up. The two classes I talked about:

Optics: (note line 64. this line returns a null intersection when it shouldn't)

package ofer.davidson;
 import java.util.ArrayList;
 public class Optics {
 //Arrays to store all the optics objects and rays that are created
ArrayList<Object> objects;
ArrayList<Ray> rays;
 
[Code] .....

Also why doesn't my background turn white??

View Replies View Related

Error - ArrayList Has Private Class

Jan 30, 2015

Java Code:

import java.util.Scanner;
import java.util.ArrayList;
public class Problem1
{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
ArrayList<String> list = new ArrayList<String>();

[Code] ....

There is an error and says that my ArrayList has private access. I can't figure out how to fix it.

The code runs but when I enter "Quit", the program just stops. The arraylist isn't printed out?

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved