我在我的代码中得到nullpointerexception ..不知道该代码中有什么错误 [英] I am getting nullpointerexception in my code..don't know whats wrong in that code

查看:65
本文介绍了我在我的代码中得到nullpointerexception ..不知道该代码中有什么错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到Null指针异常...不知道该代码中有什么错误...如何解决它。



我是什么尝试过:



I am getting Null pointer exception...don't know whats wrong in that code...how to resolve it.

What I have tried:

import java.util.Scanner;

class Node
{
	private int data;
	private Node next;
	

	
	public Node(int d , Node n)
	{
		data = d;
		next = n;
	}
	
	public void setData(int d)
	{
		data = d;
	}
	
	public void setNext(Node n)
	{
		next = n;
	}
	
	public int getData()
	{
		return(data);
	}
	
	public Node getNext()
	{
		return(next);
	}
}

class Linkedlist
{
	private int size;
	private Node start;
	
	public Linkedlist()
	{
		size = 0;
		start = null;
	}
	
	public void insetAtfirst(int val)
	{
		Node n = new Node(val,start);
		
		n.setData(val);
		n.setNext(start);
		
		start = n;
		size++;
	}
	
	public void viewlist()
	{
		Node t;
		
		t = start;
		
		for(int i=1;i<=size;i++)
		{
			System.out.println(" " + t.getData());
			t = t.getNext();
		}
	}
	
	public void deleteAltNode()
	{
		
		Node t1;
		
		t1 = start;
		
		for(int i=1;i<=size;i++)
		{
			System.out.println(" " + t1.getData());
			t1 = t1.getNext().getNext();
			
		}
		
		
		size--;
		
	
		
		
	}
}



public class Delete_alternate_node 
{

	
	public static void main(String[] args)
	{
		Scanner sc = new Scanner(System.in);

		Linkedlist l = new Linkedlist();
		
		System.out.println("Enter the no of node ");
		int k = sc.nextInt();
		
		for(int i=1;i<=k;i++)
		{
			System.out.println("Enter the elements for linkedlist");
			int val = sc.nextInt();
			l.insetAtfirst(val);
		}
		
		System.out.println();
		l.viewlist();
		
		System.out.println();
		l.deleteAltNode();
		
	
		
	}

}

推荐答案

deleteAltNode 方法的逻辑是错误的。尝试

The logic of the deleteAltNode method is wrong. Try
import java.util.Scanner;

class Node
{
  private int data;
  private Node next;

  public Node(int d , Node n)
  {
    data = d;
    next = n;
  }

  public void setData(int d)
  {
    data = d;
  }

  public void setNext(Node n)
  {
    next = n;
  }

  public int getData()
  {
    return(data);
  }

  public Node getNext()
  {
    return(next);
  }
}

class Linkedlist
{
  private int size;
  private Node start;

  public Linkedlist()
  {
    size = 0;
    start = null;
  }

  public void insertAtfirst(int val)
  {
    Node n = new Node(val,start);

    n.setData(val);
    n.setNext(start);

    start = n;
    size++;
  }

  public void viewlist()
  {
    Node t;

    t = start;

    while ( t != null )
    {
      System.out.print(" " + t.getData());
      t = t.getNext();
    }
    System.out.println();
  }

  public void deleteAltNode()
  {
    Node node;
    node = start;
    while ( node != null )
    {
      Node next = node.getNext();
      if ( next != null)
      {
        node.setNext (next.getNext());
        --size;
      }
      node = node.getNext();
    }
  }
}

public class Delete_alternate_node
{
  public static void main(String[] args)
  {
    Scanner sc = new Scanner(System.in);

    Linkedlist l = new Linkedlist();

    System.out.println("Enter the no of node ");
    int k = sc.nextInt();

    for(int i=1;i<=k;i++)
    {
      System.out.println("Enter the elements for linkedlist");
      int val = sc.nextInt();
      l.insertAtfirst(val);
    }
    System.out.print("list before alternate nodes deletion: ");
    l.viewlist();

    System.out.print("list after alternate nodes deletion: ");
    l.deleteAltNode();

    l.viewlist();
  }
}


这是我们遇到的最常见问题之一,也是我们最不具备的问题之一回答,但你最有能力回答自己。



让我解释一下错误的含义:你试图使用变量,属性或方法返回值但它包含null - 这意味着变量中没有类的实例。

它有点像一个口袋:你的衬衫里有一个口袋,用来拿笔子。如果你进入口袋并发现那里没有笔,你就不能在一张纸上签名 - 如果你尝试的话,你会得到非常有趣的外观!空口袋给你一个空值(这里没有笔!)所以你不能做任何你检索笔通常做的事情。它为什么空?这就是问题 - 可能是你今天早上离开家时忘了拿起你的笔,或者你昨晚把它拿到昨天的衬衫口袋里时可能会把笔留下来。



我们无法分辨,因为我们不在那里,更重要的是,我们甚至看不到你的衬衫,更不用说口袋里的东西了!



回到计算机,你做了同样的事情,不知何故 - 我们看不到你的代码,更不用说运行它了,找不到包含null的东西。

但你可以 - 你的IDE会在这里帮助你。在调试器中运行您的程序,当它失败时,调试器将向您显示它发现问题的行。然后,您可以开始查看它的各个部分,以查看哪个值为null,并开始回顾代码以找出原因。因此,在包含错误行的方法的开头放置一个断点,然后从头再次运行程序。这一次,执行将在错误发生之前停止,让您通过单步查看您的值来检查发生了什么。具体如何使用调试器将取决于您使用的IDE,但快速谷歌的IDE名称加上调试器教程应该可以获得您需要的信息。



但我们不能这样做 - 我们没有您的代码,如果我们拥有它,我们不知道如何使用它,我们没有您的数据。所以尝试一下 - 看看你能找到多少信息!
This is one of the most common problems we get asked, and it's also the one we are least equipped to answer, but you are most equipped to answer yourself.

Let me just explain what the error means: You have tried to use a variable, property, or a method return value but it contains null - which means that there is no instance of a class in the variable.
It's a bit like a pocket: you have a pocket in your shirt, which you use to hold a pen. If you reach into the pocket and find there isn't a pen there, you can't sign your name on a piece of paper - and you will get very funny looks if you try! The empty pocket is giving you a null value (no pen here!) so you can't do anything that you would normally do once you retrieved your pen. Why is it empty? That's the question - it may be that you forgot to pick up your pen when you left the house this morning, or possibly you left the pen in the pocket of yesterdays shirt when you took it off last night.

We can't tell, because we weren't there, and even more importantly, we can't even see your shirt, much less what is in the pocket!

Back to computers, and you have done the same thing, somehow - and we can't see your code, much less run it and find out what contains null when it shouldn't.
But you can - and your IDE will help you here. Run your program in the debugger and when it fails, the debugger will show you the line it found the problem on. You can then start looking at the various parts of it to see what value is null and start looking back through your code to find out why. So put a breakpoint at the beginning of the method containing the error line, and run your program from the start again. This time, execution will stop before the error, and let you examine what is going on by stepping through the code looking at your values. Exactly how you use the debugger will depend on the IDE you are using, but a quick google for the name of your IDE plus "debugger tutorial" should get you the info you need.

But we can't do that - we don't have your code, we don't know how to use it if we did have it, we don't have your data. So try it - and see how much information you can find out!


你的代码没有你想象的那样,或者你不明白为什么!



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

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

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

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

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



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

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



http://docs.oracle .com / javase / 7 / docs / technotes / tools / windows / jdb.html [ ^ ]

https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html [ ^ ]



这里的调试器只显示你的代码正在做什么,你的任务是与它应该做什么进行比较。
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[^]

http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

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天全站免登陆