Java StackOverflowError [英] Java StackOverflowError

查看:56
本文介绍了Java StackOverflowError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

...
float value = Float.parseFloat((String)model.getValueAt(e.getLastRow(), 1));             
DecimalFormat dec = new DecimalFormat("#.###");
model.setValueAt(dec.format(value), e.getLastRow(), 1);
...

在第三行,我得到了stackOverflowError异常.我打算做的是从对象中获取JTable单元格值,将其转换为浮点数,将其限制为3个小数位,最后转换为String并将该值设置为3个小数位.

at the third line i'm getting the stackOverflowError exception. What I'm intending to do is getting a JTable cell value from an Object, converting it to a float, limiting it to 3 decimal places, and finally convert to String and set the value with 3 decimal places at the cell.

我想问题是我正在更改值,并一次又一次地输入该函数.因此,StackOverflow是由于该原因.问题是,我该如何解决?

I guess the problem is I'm changing the value, and entering the function again and again. So the StackOverflow is due to that. Question is, how can i fix this?

位于以下位置的完整功能: Java:转换数据类型 (很抱歉两次发帖……这是一个不同的问题,解决方案使我陷入了另一个问题)

Complete function at: Java: Converting data types (Sorry for posting twice... It was a different question, and the solution drove me to a different problem)

推荐答案

问题是setValueAt()作为实现的一部分,将在所有已注册的侦听器(包括该侦听器)上调用tableChanged().

The problem is that setValueAt() will, as part of its implementation call tableChanged() on all registered listeners, including this one.

为避免这种情况,只需检查单元格中的值是否已经为所需格式,这是方法中的第一件事,如果已执行,则不执行任何操作.

In order to avoid this, simply check whether the value in the cell is already in the desired format as the first thing in your method, and don't do anything if it is.

这篇关于Java StackOverflowError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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