如何消除此示例代码中的警告。
我将 Eclipse Neon 与 Java 1.8 和 org.eclipse.jdt.annotation_2.1.0 结合使用
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
@NonNullByDefault
public class NullAnnotationTest4 {
public static void main(String[] args) {
final TreeMap<@Nullable Integer, @Nullable String> treeMap = new TreeMap<>();
treeMap.put(3, "Test1");
treeMap.put(null, null);
//This produces the warning
final Set<@Nullable Entry<@Nullable Integer, @Nullable String>> set = treeMap.entrySet();
for (final Iterator<@Nullable Entry<@Nullable Integer, @Nullable String>> it = set.iterator(); it.hasNext(); ) {
final Entry<@Nullable Integer, @Nullable String> entry = it.next(); …Run Code Online (Sandbox Code Playgroud) 在Java中,我有以下方法:
public Optional<Foo> getFoo() {
// always return some non-null value
}
Run Code Online (Sandbox Code Playgroud)
在Kotlin代码中,此方法的返回类型为Optional<Foo!>!。通过使用@Nonnull注释,我可以将其缩减为Optional<Foo!>(即,仅Foo不再对类型进行空检查)。
有没有办法注释使Kotlin编译器对返回值正确进行null检查的方法?
我正在尝试在 C# 中创建一个模型类,其中我需要对象/列表属性作为可选属性:
public class Customer
{
[JsonProperty("Custid")]
public string CustId { get; set; }
[JsonProperty("CustName")]
public string CustName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
public class Store
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("Name")]
public string? Name { get; set; }
[JsonProperty("Customer")]
public List<Customer>? Customers{ get; set; } *//Error 1*
[JsonProperty("OtherProperty")]
public object? OtherProperty{ get; set; } *//Error 2*
}
Run Code Online (Sandbox Code Playgroud)
上面的代码给出的错误为:-
错误 1:类型“对象”必须是不可为空的值类型,才能将其用作泛型类型或方法“可空”中的参数“T”
错误 2:类型“ List' 必须是不可为 null 的值类型,以便将其用作泛型类型或方法 'Nullable' 中的参数 'T'
请向我解释上述情况并为我提供替代解决方案。
我有一个为以前版本的 Flutter 编写的代码,当我尝试在新版本中运行时出现一些错误。以下是我不知道如何解决的错误之一?
Future<void> deleteProduct(String id) async {
final url = Uri.parse(
'https://flutter-update.firebaseio.com/products/$id.json?auth=$authToken');
final existingProductIndex = _items.indexWhere((prod) => prod.id == id);
var existingProduct = _items[existingProductIndex];
_items.removeAt(existingProductIndex);
notifyListeners();
final response = await http.delete(url);
if (response.statusCode >= 400) {
_items.insert(existingProductIndex, existingProduct);
notifyListeners();
throw HttpException('Could not delete product.');
}
existingProduct = null;
}
Run Code Online (Sandbox Code Playgroud)
代码最后一行出现的错误消息是:
无法将“Null”类型的值分配给“Product”类型的变量。尝试更改变量的类型,或将右侧类型强制转换为“Product”。
编辑:除了解决我的问题的答案之外,我注意到我还可以在以下代码行中dynamic编写:Product?
var existingProduct = _items[existingProductIndex];
Run Code Online (Sandbox Code Playgroud)
我很想知道哪种解决方案更好?为什么?
我正在尝试编译大师“Joe Duffy”为类生产者/消费者提出的代码(更多迭代器乐趣与生产者/消费者模式),但发生了此错误:
(我使用的是 Visual Studio 2010 和 Net 4.0.3)
Program.cs(37,34):错误CS0453:类型“T”必须是不可空值类型才能将其用作泛型类型或方法“System.Nullable”中的参数“T” Program.cs(11,40):(相关位置) Program.cs(37,61):错误CS0453:类型“T”必须是不可空值类型才能将其用作泛型类型或方法“System.Nullable”中的参数“T” Program.cs(11,40):(相关位置) Program.cs(44,53):错误CS0453:类型“T”必须是不可空值类型才能将其用作泛型类型或方法“System.Nullable”中的参数“T” Program.cs(11,40):(相关位置)
对于我浅薄的知识来说实在是太多了!有人可以提出解决方案吗?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace ProducerConsumerClass
{
class Program
{
public abstract class Producer<T>
{
public Producer()
{
worker = new Thread(new ThreadStart(this.ProductionCycle));
}
private Queue<T> buffer = new Queue<T>();
public Thread worker;
private bool done;
public bool Done
{
get
{
return done;
}
}
public IEnumerable<T> ConsumerChannel
{
get
{
if (done)
throw new InvalidOperationException("Production …Run Code Online (Sandbox Code Playgroud) 我写了这个简单的实用函数:
public static T isNull<T>(T? v, T d)
{
return v == null ? d : v.Value;
}
Run Code Online (Sandbox Code Playgroud)
目的是避免烦人的任务,例如检查成员是否为null,在读取linq记录集时非常常见.问题是它抛出了这个错误:
类型'T'必须是非可空值类型,以便在泛型类型或方法'System.Nullable <T>'中将其用作参数'T'
错误似乎是合法的,无论如何我希望我可以这样做:
int? myField = record.myField;
int myValue = isNull(myField, 0);
Run Code Online (Sandbox Code Playgroud)
代替:
int? myField = record.myField;
int myValue = myField == null ? 0 : myField.Value;
Run Code Online (Sandbox Code Playgroud)
我觉得我缺少一些c#基础知识.有没有办法完成我的任务?
如何@NonNull在List项目上使用注释。
让我们考虑一下,如果我想强制一个非空的字符串列表
我们可以这样声明: @NonNull List<String>
如果我们想强制,一个非空字符串列表。
我们怎么能做到这一点?
龙目岛的@NonNull VS javax.annotation.Nonnull
哪个更适合用于方法参数以及何时使用?
这里的答案模糊地比较了所有注释,但没有推断出哪一个最好。我只是想知道两者中哪个更好。
假设我有一个包含x成员的数组,我想创建另一个具有相同Length(x)和相同整数值的数组:
int[] arr = new int[x];
int?[] nullable_arr = arr;
Run Code Online (Sandbox Code Playgroud)
我不能这样做:
int?[] arr = new int[x];
Run Code Online (Sandbox Code Playgroud)
是否存在显式转换为可空类型数组或我不知道的内容?
是的,我可以简单地在循环中转换每个值,但我问是否已经有一个简单的方法.
const char *const _Nonnull GetString() {
...
}
Run Code Online (Sandbox Code Playgroud)
那是跨平台的吗?或者是_Nonnull编译器扩展或 c99 功能?
我正在 Android 上使用 Kotlin,有一件事让我感到困惑。
当我将一些FragmentsJava 转换为 Kotlin 时,我得到了这个:
class XFragment : Fragment() {
private var binding: FragmentXBinding? = null
override fun onCreateView(inflater: LayoutInflater,
container: ViewGroup?, savedInstanceState: Bundle?): View? {
binding = FragmentUhfReadBinding.inflate(inflater, container, false)
return binding!!.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding!!.slPower.addOnChangeListener(this)
binding!!.btnClearTagList.setOnClickListener(this)
}
// ...
private fun updateUi(){
binding!!.someTextView.text = getSomeTextViewText()
binding!!.someSlider.value = getSomeSliderValue()
}
}
Run Code Online (Sandbox Code Playgroud)
我无法使其binding不可为空,因为它必须XFragment在类构造函数之后(onCreateView()或之后)进行初始化。
因此,通过这种方法,它必须可以为空,并且我必须将其放在!!任何地方。
有什么方法可以避免这些!!吗?