请帮忙:循环链表nullpointerexception错误 [英] Please help: circular linked list nullpointerexception error

查看:92
本文介绍了请帮忙:循环链表nullpointerexception错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在print语句中出现NullPointerException错误,它会在驱动程序中打印我输入和需要的所有内容。但是它之后会导致NullPointException错误。我需要这个工作,因为这个错误我不能再做任何方法。



我对这个tmr进行了测试,我不知道为什么会导致这个异常错误。请帮忙!谢谢。



我尝试了什么:



Getting a NullPointerException error in the print statement, it is printing everything i input and need in the driver. But right after it causes the NullPointException error. I need this to work, I can not make any more methods afterwards because of this error.

I have a test on this tmr and I have no clue why it's causing this exception error. Please help! Thank you.

What I have tried:

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

public class StringNode 
{
   //attributes
   private String data;
   private StringNode link;
   //nodes counter
   int numNodes =0;
   
   Scanner kb = new Scanner(System.in);
   
   //constructor
   public StringNode(String iData, StringNode iLink)
   {
      data = iData;
      link = iLink;
   }
   
   //create empty list 
   StringNode list = null;
   
   
   
   //*********Create method: addToFrontFILI
   public void addToFrontBM(String newN)
   {
      list = new StringNode(newN, list);
      System.out.println("A node has been added to the front.");
      numNodes++;
      System.out.println("NumNodes:" +numNodes);
   }

   //---------------------Works but causes Nullpointer Exception------------
   //Create method: printLLFILI
   public void printLLBM() 
   {
      StringNode tptr = list;
      
      while(tptr.link != list)
      {
         System.out.println(tptr.data);
         tptr = tptr.link;
      }
      
      System.out.println(tptr.data);
      
   }

推荐答案

引用:

在print语句中出现NullPointerException错误,它正在打印我在驱动程序中输入和需要的所有内容。但是在它导致NullPointException错误之后。

Getting a NullPointerException error in the print statement, it is printing everything i input and need in the driver. But right after it causes the NullPointException error.



我们不能使用这段代码重现问题,因为它不是自治的,我们无法运行该代码。

更改代码,使其在没有用户输入的情况下运行并生成问题。


We can'tuse this piece of code to reproduce the problem because it is not autonomous, we can't run that code.
Change the code so that it run and generate problem without user input.

Quote:

I对此tmr进行测试,我不知道为什么会导致此异常错误。

I have a test on this tmr and I have no clue why it's causing this exception error.



您的代码行为不符合您的预期,或者您不明白为什么!



有一个几乎通用的解决方案:一步一步地在调试器上运行你的代码,检查变量。

调试器在这里向你展示你的代码正在做,你的任务是与它应该做的事情进行比较。

调试器中没有魔法,它不知道你的代码应该做什么,它没有找到bug ,它只是通过向您展示正在发生的事情来帮助您。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]


掌握调试Visual Studio 2010 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]



在Visual Studio中调试C#代码 - YouTube [ ^ ]



这里的调试器只显示你的代码正在做什么,你的任务是与它应该做什么进行比较。


Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

Debugging C# Code in Visual Studio - YouTube[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.


这篇关于请帮忙:循环链表nullpointerexception错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆