Java 迭代中出现空指针异常,无法搜索节点。delete和replace方法是有问题的方法
您触发自己的NPE:Java 迭代中出现空指针异常,无法搜索节点。delete和replace方法是有问题的方法,java,Java,您触发自己的NPE: import javax.swing.*; import java.io.*; class MyDatabase implements Database { Node head = null, tail = null, rover = null; String ako; File myFile = new File("sample.dat"); Node n = new Node(); Node current; Node p; Node x = new Node();
import javax.swing.*;
import java.io.*;
class MyDatabase implements Database {
Node head = null, tail = null, rover = null;
String ako;
File myFile = new File("sample.dat");
Node n = new Node();
Node current; Node p;
Node x = new Node();
public void insert(Node myNewNode) {
if (head == null){
head = myNewNode;
head.next = null;
}
else {
tail = head;
while(tail.next != null)
tail = tail.next;
tail.next = myNewNode;
myNewNode.next = null;
}
current = head;
}
public boolean delete(Node nodeToDelete) {
//the delete and replace methods are the ones that have problems
current = head;
p = head;
head = null;
//here, no matter what you enter, this if statement is never executed. Yes, never. even if they are equal.
if(nodeToDelete.title == head.title) {
head = head.next;
return true;
}
else{
while(current != nodeToDelete)
current = current.next;//Null Pointer exception here
while(p.next != nodeToDelete)
p = p.next;//Null Pointer exception here
current = current.next;
p = current;
}
current = head;//this is for listIterator purposes.
return true;
}
public boolean replace(Node nodeToReplace, Node myNewNode) {
//the delete and replace methods are the ones that have problems
//here i tested if the head.title and nodeToReplace.title have values
//the println correctly prints the value that I input
current = head;
String s = head.title;// for example i entered "max"
String s1 = nodeToReplace.title;// i also entered "max"
System.out.println(s);//prints out "max"
System.out.println(s1);// prints out "max"
if(s == s1) { // if statement is not executed. Note: i entered the same string.
myNewNode.next = head.next;
head = myNewNode;
}
else {
while(current != null) {
String s2 = current.title;
if(s2 == s1) {
current = new Node(myNewNode);
}
}
}
current = head;
return true;
}
public Node search(Node nodeToSearch) {
current = head;
while(current != null) {
if(current == nodeToSearch) {
Node p = new Node(current);
current = head;
return p;
}
}
return null;
}
public boolean saveToFile(String filename) throws Exception {
Node p = new Node();
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(myFile));
out.writeObject(p);
out.close();
return true;
}
public boolean loadFromFile(String filename) throws Exception {
ObjectInputStream in = new ObjectInputStream(new FileInputStream(myFile));
head = (Node) in.readObject();
return true;
}
public Node listIterator() {
try{
if(current == head) {
rover = current;
current = current.next;
return rover;
}
else {
rover = current;
current = current.next;
Node p = new Node(rover);
return p;
}
}
catch(NullPointerException e) {
current = head;
return null;
}
}
public Node listIterator2() {
try{
if(current == head) {
rover = current;
current = current.next;
return rover;
}
else {
rover = current;
current = current.next;
return rover;
}
}
catch(NullPointerException e) {
current = head;
return null;
}
}
public boolean equals(Database db) {
Node p;
while(rover != null) {
p = head;
while(p != null) {
if(rover != p)
return false;
p = p.next;
}
rover = rover.next;
}
return true;
}
public String whoIAm() {
ako = "Michael Glenn R. Roquim Jr. !";
return ako;
}
}
您触发自己的NPE:
import javax.swing.*;
import java.io.*;
class MyDatabase implements Database {
Node head = null, tail = null, rover = null;
String ako;
File myFile = new File("sample.dat");
Node n = new Node();
Node current; Node p;
Node x = new Node();
public void insert(Node myNewNode) {
if (head == null){
head = myNewNode;
head.next = null;
}
else {
tail = head;
while(tail.next != null)
tail = tail.next;
tail.next = myNewNode;
myNewNode.next = null;
}
current = head;
}
public boolean delete(Node nodeToDelete) {
//the delete and replace methods are the ones that have problems
current = head;
p = head;
head = null;
//here, no matter what you enter, this if statement is never executed. Yes, never. even if they are equal.
if(nodeToDelete.title == head.title) {
head = head.next;
return true;
}
else{
while(current != nodeToDelete)
current = current.next;//Null Pointer exception here
while(p.next != nodeToDelete)
p = p.next;//Null Pointer exception here
current = current.next;
p = current;
}
current = head;//this is for listIterator purposes.
return true;
}
public boolean replace(Node nodeToReplace, Node myNewNode) {
//the delete and replace methods are the ones that have problems
//here i tested if the head.title and nodeToReplace.title have values
//the println correctly prints the value that I input
current = head;
String s = head.title;// for example i entered "max"
String s1 = nodeToReplace.title;// i also entered "max"
System.out.println(s);//prints out "max"
System.out.println(s1);// prints out "max"
if(s == s1) { // if statement is not executed. Note: i entered the same string.
myNewNode.next = head.next;
head = myNewNode;
}
else {
while(current != null) {
String s2 = current.title;
if(s2 == s1) {
current = new Node(myNewNode);
}
}
}
current = head;
return true;
}
public Node search(Node nodeToSearch) {
current = head;
while(current != null) {
if(current == nodeToSearch) {
Node p = new Node(current);
current = head;
return p;
}
}
return null;
}
public boolean saveToFile(String filename) throws Exception {
Node p = new Node();
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(myFile));
out.writeObject(p);
out.close();
return true;
}
public boolean loadFromFile(String filename) throws Exception {
ObjectInputStream in = new ObjectInputStream(new FileInputStream(myFile));
head = (Node) in.readObject();
return true;
}
public Node listIterator() {
try{
if(current == head) {
rover = current;
current = current.next;
return rover;
}
else {
rover = current;
current = current.next;
Node p = new Node(rover);
return p;
}
}
catch(NullPointerException e) {
current = head;
return null;
}
}
public Node listIterator2() {
try{
if(current == head) {
rover = current;
current = current.next;
return rover;
}
else {
rover = current;
current = current.next;
return rover;
}
}
catch(NullPointerException e) {
current = head;
return null;
}
}
public boolean equals(Database db) {
Node p;
while(rover != null) {
p = head;
while(p != null) {
if(rover != p)
return false;
p = p.next;
}
rover = rover.next;
}
return true;
}
public String whoIAm() {
ako = "Michael Glenn R. Roquim Jr. !";
return ako;
}
}
您将head设置为null,然后立即尝试访问它。。。(head始终为空,因此head.title将抛出NPE)
此外:
head = null;
//here, no matter what you enter, this if statement is never executed. Yes, never. even if they are equal.
if(nodeToDelete.title == head.title) {
在比较字符串时使用equals()
,而不是==(因为否则您要查找的是相同的精确引用,而不是相同的“字符串”)
还有一件事:如果元素不在列表中,您似乎总是抛出NPE(删除时)(您将找不到该元素,到达列表的末尾(为null),然后尝试处理其中的实例变量) 您将head设置为null,然后立即尝试访问它。。。(head始终为空,因此head.title将抛出NPE)
此外:
head = null;
//here, no matter what you enter, this if statement is never executed. Yes, never. even if they are equal.
if(nodeToDelete.title == head.title) {
在比较字符串时使用equals()
,而不是==(因为否则您要查找的是相同的精确引用,而不是相同的“字符串”)
还有一件事:如果您的元素不在列表中,您似乎总是会抛出NPE(删除时)(您将找不到元素,到达列表末尾为null,然后尝试解决其中的实例变量).我喜欢将代码简化为基本内容,因此这里有一个工作版本,它只按照方法的建议执行
if(s == s1) { // if statement is not executed. Note: i entered the same string.
import java.io.*;
导入java.util.ArrayList;
导入java.util.List;
导入java.util.ListIterator;
类MyDatabase{
私有最终列表标题=新ArrayList();
公开作废插入(书名){
标题。添加(标题);
}
公共布尔删除(书名){
返回标题。删除(标题);
}
公共布尔替换(书籍标题1,书籍标题2){
int pos=标题索引(标题1);
如果(位置>=0){
标题集(位置,标题2);
返回true;
}
返回false;
}
公共图书搜索(字符串标题){
用于(书名){
如果(书名等于(书名))
还书;
}
返回null;
}
public void saveToFile(字符串文件名)引发IOException{
ObjectOutputStream out=新的ObjectOutputStream(新文件输出流(文件名));
out.书面对象(标题);
out.close();
}
public void loadFromFile(字符串文件名)引发IOException,ClassNotFoundException{
ObjectInputStream in=新ObjectInputStream(新文件输入流(文件名));
标题。清除();
titles.addAll((列表)在.readObject()中);
in.close();
}
公共ListIterator ListIterator(){
返回titles.listIterator();
}
公共布尔等于(对象o){
返回o MyDatabase的instanceof&((MyDatabase)o).titles.equals(titles);
}
公共字符串whoIAm(){
返回“小迈克尔·格伦·R·罗奎姆!”;
}
}
课堂用书{
最后的字符串标题;
最后一年;
书籍(字符串标题,整数年){
this.title=标题;
今年=年;
}
@凌驾
公共布尔等于(对象o){
如果(this==o)返回true;
如果(o==null | | getClass()!=o.getClass())返回false;
簿册=(簿册)o;
如果(年!=账面年)返回false;
如果(!title.equals(book.title))返回false;
返回true;
}
}
我喜欢将代码简化为基本内容,因此这里有一个工作版本,它只按照方法的建议执行
if(s == s1) { // if statement is not executed. Note: i entered the same string.
import java.io.*;
导入java.util.ArrayList;
导入java.util.List;
导入java.util.ListIterator;
类MyDatabase{
私有最终列表标题=新ArrayList();
公开作废插入(书名){
标题。添加(标题);
}
公共布尔删除(书名){
返回标题。删除(标题);
}
公共布尔替换(书籍标题1,书籍标题2){
int pos=标题索引(标题1);
如果(位置>=0){
标题集(位置,标题2);
返回true;
}
返回false;
}
公共图书搜索(字符串标题){
用于(书名){
如果(书名等于(书名))
还书;
}
返回null;
}
public void saveToFile(字符串文件名)引发IOException{
ObjectOutputStream out=新的ObjectOutputStream(新文件输出流(文件名));
out.书面对象(标题);
out.close();
}
public void loadFromFile(字符串文件名)引发IOException,ClassNotFoundException{
ObjectInputStream in=新ObjectInputStream(新文件输入流(文件名));
标题。清除();
titles.addAll((列表)在.readObject()中);
in.close();
}
公共ListIterator ListIterator(){
返回titles.listIterator();
}
公共布尔等于(对象o){
返回o MyDatabase的instanceof&((MyDatabase)o).titles.equals(titles);
}
公共字符串whoIAm(){
返回“小迈克尔·格伦·R·罗奎姆!”;
}
}
课堂用书{
最后的字符串标题;
最后一年;
书籍(字符串标题,整数年){
this.title=标题;
今年=年;
}
@凌驾
公共布尔等于(对象o){
如果(this==o)返回true;
如果(o==null | | getClass()!=o.getClass())返回false;
簿册=(簿册)o;
如果(年!=账面年)返回false;
如果(!title.equals(book.title))返回false;
返回true;
}
}
嗯,这里有几个问题