尝试打印对象时获得空指针异常 [英] getting null pointer exception when trying to print object

查看:378
本文介绍了尝试打印对象时获得空指针异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是相关的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.

这篇关于尝试打印对象时获得空指针异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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