Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
下面是相关代码:
WebPage obPage1 = new WebPage(null, null, 0); obPage1.setUrl(page1.substring(st + 4, en)); System.out.println(obPage1.toString());
这给了我一个空指针异常,知道我哪里出错了吗?
WebPage 是您编写的自定义类吗?
即使您没有实现 toString 方法,它仍应返回有效字符串,因为它将扩展 Object 的 toString 方法
getClass().getName() + '@' + Integer.toHexString(hashCode())
确定代码在做什么的最快方法是在调试器模式下运行它,这样您就可以知道 WebPage 中存储的内容。
从示例中也可以看出,您希望 toString 返回页面的 url。如果是这样,只需为 url 创建一个 getter。