java字符串替换空指针异常跟,迭代中无空指针异常且无法搜索节点。删除和替换方法是有问题的方法...

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;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值