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

2023-09-04 04:26:58 作者:不晤面不怀念

下面是相关的code:

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?

推荐答案

是好康你写了一个自定义类?

Is WebPage a custom class you wrote?

即使你没有实现一个toString方法,它仍然应该返回有效的字符串,因为这将是扩大对象的ToString方法

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

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

最快捷的方法,以确定哪些code正在做的是运行它的调试器模式,这样你可以什么存储里面的网页。

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

看起来也像是从您要的toString返回一个页面的URL的例子。如果多数民众赞成的情况下,只是做一个getter方法​​的URL。

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.