Getting Null Pointer Error In Double Linked List Project

May 25, 2014

I am currently making a double linked list class in my compsci class. i was absent for a few days and i need to make an add method for the class. when i go to compile test code i made i get a null pointer error on line 36. This is the node class i wrote and the double linked list class i wrote.

node class

public class Node{
// Two references and Data
Node prev;
Node next;
String data;
public Node(){
prev = null;
next = null;

[Code] ....

This is the class i need working with ( in the doublelinkedlist class)

public void add(Node n){
if(isEmpty()){
first = n;
return;
}
Node temp = first;
while(temp != null){
temp = temp.getNext();
}
temp.setNext(n);
temp.setPrev(temp);
}

View Replies


ADVERTISEMENT

Linked List With Random Pointer

Feb 5, 2014

A "postings list" has a data field, a field for the next pointer and a jump field - the jump field points to any other node. The last node in the postings list has next set to null; all other nodes have non-null next and jump fields.

Implement a function that a postings list as input and returns a copy of the list, in O(n) time and O(1) storage beyond that required for the nodes in the copy. You may modify the original list but must return it to its original state before returning.

I was able to find a solution, but not one that runs in O(n) time.

View Replies View Related

Null Pointer Error

Oct 15, 2014

So I'm trying to add two numbers using a stack class I made myself. I have a main class with a main method in which I use three stacks on holds one number one holds the other and then I sum one digit at a time keeping in midn that there may be a carry before putting the result on a third stack.

My problem is I'm getting a null pointer error when it should be putting an integer into the top of my stack which is index 0, I've checked my code and can't see why it would be doing this.This is my main method in my main class

import java.util.*;
import java.io.*;
public class CS25driver {
public static void main(String[]args) throws FileNotFoundException {
 
[code]...

my input file looks like a single line of input stating the problem followed by two lines of input which need to be summed.this is the error I'm getting

Exception in thread "main" java.lang.NullPointerException
at StackClass.push(StackClass.java:38)
at CS25driver.main(CS25driver.java:55)

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

Run Time Error As Null Pointer Exception While Compiling

Apr 12, 2015

I have an input text file as file.txt where, it consists of only real values separated with commas, and arranged as rows as columns as mentioned below :

1,2,3,4,5,6,10,5,8,9
1,4,7,8,9,0,6,4,3,2,1
2,4,5,8.1.3.4.6.7.9.0

the number of rows is 100 and the number of columns are 9, that is they are fixed.Above is just an example, the file consists of decimal values.What I require is for each column of values I need to find out it's average or mean, that is in above example,

for 1st column:(1+1+2)/3
for 2nd column(2+4+4)/3
for 3rd column(3+7+5)/3,,,,,,,like this till the last column.

So I need to take the input from the .txt file, calculate the column average of each column and the average of each column must be displayed as output. I have done a bit of coding, but it is giving a run-time error as NullPointerException.I will forward my code as attachment, the reading of file, is done as string in java and then it must be converted to double values, also to read the comma separated input from a file I have used the split().

View Replies View Related

Calculate Button Returns Null Pointer Error

Mar 31, 2014

I'm using NetBeans to create this project and I have the gui registering text boxes, but it won't retrieve what the user entered, so the calculate button returns a null exception error.

Java Code:

package payroll;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import javax.swing.JButton;
import javax.swing.JComboBox;

[Code] .....

View Replies View Related

Binary Expression Tree Print - Null Pointer Error

Aug 3, 2014

I am trying to build an expression tree program . I try to print it then I have java.nullpointer exception . Is it error in my code or what ?

I have 2 classes

one is BSTreeNode which is the nodes for the tree . It has

public class ExpressionTree {
public BSTreeNode root;
public BSTreeNode curr = root;
ExpressionTree() {
root = new BSTreeNode(null, null, null, null);
curr = root;

[Code] ....

My BSTreeNode

public class BSTreeNode {
BSTreeNode parent;
Object element; // Binary search tree element
BSTreeNode left; // Reference to the left child
BSTreeNode right; // Reference to the right child
// Constructor
BSTreeNode (Object elem)
[Code] ....

The error i am getting is

Exception in thread "main" java.lang.NullPointerException
at binaryexpressiontree.ExpressionTree.insert(ExpressionTree.java:38)
at binaryexpressiontree.test.main(test.java:22)

Java Result: 1

After implementing exception handler what i am getting is

java.lang.NullPointerException
root 1 *
root 2 1 error error error//these error print because of my catch clause

View Replies View Related

Program A Star Algorithm Using JButtons - Null Pointer Error

May 30, 2014

I am trying to program the A star algorithm using JButtons. Now I have some tweaking to do but I have to get past the errors first.

I am getting the following errors:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at AStar.findEndButton(AStar.java:40)
at Screen$1.actionPerformed(Screen.java:59)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)

[Code] ....

In my AStar class you will see these buttons which is an instance of matrixButtons

MatrixButtons[][] current;
MatrixButtons[][] endButton;
MatrixButtons[][] startButton;
MatrixButtons[][] nextCurrent;
MatrixButtons[][] temp;
MatrixButtons bestNextMove;

I really need them to handle several different methods. A full example of the full program is below.

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
 
import javax.swing.*;
public class Screen

[Code] ....

View Replies View Related

Removing Item From String Array - Null Pointer Exception Error

Jun 8, 2014

Question 1: I am working on an assignment where I have to remove an item from a String array (see code below). When I try to remove an item after entering it I get the following error "java.lang.NullPointerException." I am not sure how to correct this error.

Question 2: In addition, I am having trouble figuring out how to count the number of occurrences of each string in the array and print the counts. I've been looking at other posts but they are more advanced and I have not yet learned how to use some of the tools they are referring to.

private void removeFlower(String flowerPack[]) {
// TODO: Remove a flower that is specified by the user
Scanner input=new Scanner(System.in);
System.out.println();
System.out.println("Please enter the name of the flower you would like to remove:

[Code] ....

View Replies View Related

Infinite Loop On Double Linked List

Sep 11, 2014

I've been stuck for the last couple hours trying to understand why the "printInReverse" method is getting into an infinite loop. I was supposed to make a double linked list that you can insert numbers and it orders both ascending and descending; in this case, descending is the "printInReverse" method, which takes the already ordered lists and prints it reversed, like if it was descending, and that's where the problem lives. Here follows the whole code:

import java.util.Scanner;
public class MyList {
private Scanner userInput;
private Integer selectedOption;
private MyListElement firstElement = null;
private boolean exitRequested = false;
 
[Code] .....

View Replies View Related

Linked List Sort - ToString Returning Null

May 22, 2014

import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.lang.Comparable;
import java.util.*;
import java.lang.*;
public class MyLinkedListSort {

[Code] .....

Output is :

name is nulland salary is0
name is nulland salary is0
name is nulland salary is0
name is nulland salary is0

Expected output :

Name is Crish Salary: 2000
Name is Tom Salary: 2400
Name is Ram Salary: 3000
Name is John Salary: 6000

Where i going wrong

View Replies View Related

Double Circular Linked List - Implementing Iterator

Oct 14, 2014

My homework is a Double Circular Link list and when i write implements Iterator it gives me a an error when I compile it the same for my subset method...

ERRORS :DoublyCircularList.java:55: error: DoublyCircularList.iterator is not abstract and does not override abstract method remove() in Iterator public class iterator implements Iterator<T>
^
Note: DoublyCircularList.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error

import java.util.Iterator;
public class DoublyCircularList<T extends Comparable<T>> extends LinkedList<T>implements Iterable<T>
{
Node<T> first;
int size;
public DoublyCircularList(){
first = null;
size = 0;

[code]....

View Replies View Related

Reverse Double Linked List In Java - Descending Order

Sep 12, 2014

I have the following double linked list and I'm supposed to order it descending (reverse) using the printInReverse() method; since the list orders itself ascending when the numbers are added, how could I order it descending in this method? Here's the code without implementing descending/reversing methods:

package test;
import java.util.Scanner;
public class MyList {
private Scanner userInput;
private Integer selectedOption;
private MyListElement firstElement = null;
private boolean exitRequested = false;

[Code] ....

I tried to declare a previousElement variable but I didn't figure out how I'd do that.

View Replies View Related

Sorting Ascending / Descending Methods Not Working On Double Linked List

Sep 12, 2014

I wrote displayAscending() and displayDescending() methods to this double linked list and it is not working at all. Logically it seems fine to me. I positioned the head in the beginning in the ascending method; created a variable named data1 as an auxiliar variable so it can store the values that are going to be moved; and moved the values. Same thing for the descending method but instead of the head I put the tail and move left the list, instead of right.

import java.util.*;
class node {
int data;
node left;
node right; 
node(int d, node l, node r) {
data = d;

[Code]...

View Replies View Related

Null Pointer Access Variable Can Only Be Null At This Location

Apr 16, 2014

I'm getting an error on line 137 and all it is rsmd = rs.getMetaData();The error happens only after I press a JButton and it is

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerExceptionat softwareDesign.createAccount.actionPerformed(createAccount.java:137)

There is a lot more to it but that has nothing to do with my code it's just the stuff with Eclipse anyway I want to know how do I fix this problem?

View Replies View Related

Error In Single Linked List

Sep 13, 2014

package progProject5;
// Implementation of single linked list.
public class SingleLinkedList<E> {
private Node<E> head = null;
private int size = 0;
// Methods

[Code] ......

I am in learning process and I know its very basic question but I got stucked at addAfter() where I need to insert the node after the given node.

View Replies View Related

Dummy Node In Doubly Linked List - Cannot Find Symbol Variable Error

Jun 5, 2012

Im running into some problems with the Java compiler. This is my code:

public class DoublyLinkedList<T> implements SimpleList<T> {
protected Node dummy;
protected int n;
public DoublyLinkedList(){

dummy = new Node();
dummy.next = dummy;
dummy.pre = dummy;

n = 0;

[Code] ....

I want to use a dummy node in my implementation of the Doubly Linked List, so it will be easier to code all the methods I need to write. I keep on getting a problem with line 14 and 15:

dummy.next = dummy;

dummy.pre = dummy;

// cannot find symbol variable next (compiler error)

// cannot find symbol variable pre

View Replies View Related

Another Null Pointer Exception

Aug 14, 2014

It is supposed to create an array of random strings and sort them according to their HTML numbers. It creates the array correctly and even compiles correctly, but has a null pointer exception when it comes to the sorting. (I have cut out other parts of the program that were irrelevant).

the lines that produces the null Pointer Exception is line 24. if(array[i].charAt(0)>array[maxIndex].charAt(0))...and 53.stringSelectionSort(stringArray);

import java.util.Arrays;
import java.util.Random;
/**tests the array.sort() subroutine vs. a
'selection sort' that I will write. a random list
of integers is used first and times are recorded to
determine which one is more efficient. A random list
of strings is also tested.**/

[code]....

View Replies View Related

How Should Null Pointer Exception Be Avoided In A Program

Aug 6, 2014

How should null pointer exception be avoided in a program. Should each and every place of access of a variable be checked for null and if null is there own customized exception should be thrown. Is this approach correct for avoiding null pointer exception.

View Replies View Related

Null Pointer Exception When Trying To Write To File

Aug 14, 2014

I've been closely following a tutorial series, and as far as I can tell my code is exactly the same, but not functioning.

The app throws a nullpointer exception here on "controller.saveToFile(fileChooser.getSelectedFile ());"

// Export data item
exportDataItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (fileChooser.showSaveDialog(MainFrame.this) == JFileChooser.APPROVE_OPTION) {
try {
controller.saveToFile(fileChooser.getSelectedFile());

[Code] ....

There are no errors popping up in the code, and I dont understand why there is a null pointer exception when Im saving this file.

View Replies View Related

Null Pointer Exceptions On First Attempt At JButtons

Dec 12, 2014

I am designing/coding a blackjack GUI program and I'm trying to add function to my JButtons. Problem is, I now have 2 NullPointerExceptions.

public class GameConsole {
Player user;
Player dealer;
Deck playingDeck;
ValidateInput validate = new ValidateInput();

[Code] .....

I am trying to do. I am getting the following exception/stack trace at line 14 in userTurn()

Exception in thread "main" java.lang.NullPointerException
at blackjackControls.GameConsole.userTurn(GameConsole.java:163)
at blackjackControls.GameConsole.gamePlay(GameConsole.java:87)
at blackjackControls.Main.main(Main.java:7)

and then the program continues and after I click the button I get :

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at blackjackControls.GameConsole.userTurn(GameConsole.java:163)
at blackjackControls.ResultPanel$1.actionPerformed(ResultPanel.java:34)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2028)
//and then about 30 more "at" lines

Why am I getting these exceptions and how do I fix it?

View Replies View Related

Null Pointer Exception On Initialized Variables?

Oct 10, 2014

Take a look at this screenshot... my java integrated devlopment envirnment is telling me I have a null pointer on a line with three variables ALL which were initialized.

I thought a null pointer was called when a variable doesn't get initialized.

Edit: Here is the java file and resources being used: [URL]

View Replies View Related

Moving Circle - Null Pointer Exception

Apr 9, 2014

I am try to move circle.Below is my code...

import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;
import javax.swing.JPanel;

[Code] .....

And I have error

run:
Exception in thread "main" java.lang.NullPointerException
at swingdesing.Game.buffer(Game.java:32)
at swingdesing.Game.moveBall(Game.java:27)
at swingdesing.Game.main(Game.java:51)
BUILD SUCCESSFUL (total time: 9 seconds)

how to overcome this?

View Replies View Related

Prevent ComboBox From Being Used - Getting Null Pointer Exception

Oct 8, 2014

Should the piece of code below set prevent the comboBox from being used? All it does at the minute return a null pointer exception. See I am using the same window but I have an if statement so if a condition is true then it will change certain aspects of the window. I want it to be true if it equals export which is does and it's populated fine but when I try and hide it if the condition equals import it returns a null pointer?

comboBoxEnv.setEnabled(false);

View Replies View Related

Null Pointer Exception With File I / O And Scanner

Jul 26, 2014

I get a null pointer error on line 17 of the following fragment. I suspect it has to do with the way I am reading the file, but I'm unsure. I can provide more information or code if necessary.

JFileChooser jfc = new JFileChooser();
jfc.setFileSelectionMode( JFileChooser.FILES_AND_DIRECTORIES );
int userInput = jfc.showOpenDialog( null );
if( userInput == JFileChooser.CANCEL_OPTION ){

[Code] .....

View Replies View Related

CompareTo Statement - Null Pointer Exception

Apr 16, 2014

I am getting a NullPointerException at my compareTo statement in this section of my code. Why this is popping up?

public static int findPos(String theArray[], String playerName, int arrayCount)
{
int index;
int pos;
for(index = 0; index < arrayCount; index++) {
while ((index < arrayCount) && (playerName.compareTo(theArray[index]) > 0)) {
index++;
}
}
pos = index;
return pos;
}

Error is also creeping up on the line highlighted in a comment as "THIS LINE", and is linked to the compareTo error above.

while(yesOrNo == 'Y' || yesOrNo == 'y')
{
switch(option)
{
//Add a Player and their Stats to the Array

[Code] ....

View Replies View Related







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