尝试打印对象时获取空指针异常

时间:2021-12-17 15:43:42

Below is the relevant code:

以下是相关代码:

          WebPage obPage1 = new WebPage(null, null, 0);

          obPage1.setUrl(page1.substring(st + 4, en));

          System.out.println(obPage1.toString());

This is giving me a nullpointer exception any idea where im going wrong?

这给了我一个nullpointer异常任何想法我哪里出错?

1 个解决方案

#1


0  

Is WebPage a custom class you wrote?

WebPage是你写的自定义类吗?

Even if you didn't implement a toString method, it should still return valid string since it would be extend the Object's toString method

即使你没有实现一个toString方法,它仍然应该返回有效的字符串,因为它将扩展Object的toString方法

getClass().getName() + '@' + Integer.toHexString(hashCode())

The quickest way to determine what the code is doing is running it in debugger mode so you can whats being stored inside WebPage.

确定代码正在做什么的最快方法是在调试器模式下运行它,这样你就可以存储在WebPage中。

Also looks like from the example that you want toString to return the url of a page. If thats the case, just make a getter for the url.

从您希望toString返回页面的url的示例看起来也是如此。如果是这样的话,只需为网址制作一个getter。

#1


0  

Is WebPage a custom class you wrote?

WebPage是你写的自定义类吗?

Even if you didn't implement a toString method, it should still return valid string since it would be extend the Object's toString method

即使你没有实现一个toString方法,它仍然应该返回有效的字符串,因为它将扩展Object的toString方法

getClass().getName() + '@' + Integer.toHexString(hashCode())

The quickest way to determine what the code is doing is running it in debugger mode so you can whats being stored inside WebPage.

确定代码正在做什么的最快方法是在调试器模式下运行它,这样你就可以存储在WebPage中。

Also looks like from the example that you want toString to return the url of a page. If thats the case, just make a getter for the url.

从您希望toString返回页面的url的示例看起来也是如此。如果是这样的话,只需为网址制作一个getter。