Questions tagged [non-nullable]

232 questions
2
votes
1 answer

Annotate Type Parameter in Java for Kotlin Compiler

In Java, I have the following method: public Optional getFoo() { // always return some non-null value } In Kotlin code, the return type of this method is given as Optional!. By using the @Nonnull annotation I can cut this down to…
Bombe
  • 81,643
  • 20
  • 123
  • 127
2
votes
2 answers

Java generic with return type that has @NonNull with Collections

I want to implement a generic function in Java8, that verifies that a collection has no null members and returns a type with @NonNull annotation. input type: T extends Collection, where T+U are nullable. result type: @NonNull T, with @NonNull U For…
fbenoit
  • 3,220
  • 6
  • 21
  • 32
2
votes
0 answers

Throw compile error if @Nullable / @NonNull parameters are used incorrectly

I'm using com.android.support:support-annotations library to add @Nullable and @NonNull annotations to my project. After I switched Editor->Inspections "Constant conditions & exceptions" and "@NonNull/@Nullable problems" inspections to "Error" level…
Alexey
  • 7,262
  • 4
  • 48
  • 68
2
votes
1 answer

Mocking a class assigns nulls to @NonNull fields(Android)

I'm using Android SDK and junit4 + Mockito for unit testing. Say I have a class like this in my app: public class Container{ @NonNull private Set values = new HashSet<>(); public void addValue(String value) { values.add(value); …
Anton Cherkashyn
  • 5,719
  • 6
  • 43
  • 80
2
votes
3 answers

Hypothetical Non-Null Language - How would a Linked List be implemented?

Suppose you are writing in a programming language where null simply doesn't exist. It either uses empty objects or throws a ObjectNonexistentException or something similar. Now you want to implement a linked list. But: You can't point to null to…
2
votes
2 answers

Guava Optional and @NonNull annotation

I'm trying different ways of getting rid of NPEs in my code. I use nullness analysis in Eclipse and I'm quite fond of @NonNullByDefault. But today I run into a problem with guava's Optional: private static Optional bar() { Foo foobar = new…
Tarkis
  • 21
  • 3
2
votes
2 answers

Cannot annotate with both @NonNull and @NonNull

I am using IntelliJ 13.1.5 and Java 8 SDK and I've set up IntelliJ built-in nullness checker to use checkers.nullness.quals.NonNull as the annotation of my choice. Now, when I write a simple class like this import…
jaw
  • 932
  • 2
  • 10
  • 24
2
votes
2 answers

Create a method or list that can't receive null arguments

I have a node class that basically consists of a parent property and a list of childs. There's an AddChild method which shouldn't receive null as an argument. Users shall not add a null child. But the parent property must allow null values (a root…
Daniel Möller
  • 84,878
  • 18
  • 192
  • 214
2
votes
1 answer

What if you could create not-nullable reference type?

Have you ever faced the need of informing anyone who uses your code and passes some reference typed parameters to not pass the null? You definitely have. And you should have thrown exceptions for each "bad" variable your code cannot work with. Now,…
AgentFire
  • 8,944
  • 8
  • 43
  • 90
2
votes
3 answers

Generic typed function: return a non-nullable value

I wrote this trivial utility function: public static T isNull(T? v, T d) { return v == null ? d : v.Value; } the purpose is to avoid annoying task like check for a member to be null, very common in reading a linq recordset. The problem is…
balanza
  • 1,059
  • 1
  • 12
  • 34
2
votes
0 answers

Generating dbcontext with sqlmetal creates my table value UDF columns as nullable

I've created a inline table UDF which have some various columns of different types. ALTER FUNCTION [dbo].[fnTest]() RETURNS TABLE AS RETURN ( SELECT [t0].[type], [t0].[status], [t0].[createdDate], -- datetime not…
1
vote
1 answer

How to fix 'non-nullable property' warning in ASP.NET Razor Pages application?

Warning CS8618 Non-nullable property 'Categories_List' must contain a non-null value when exiting constructor. Consider declaring the property as nullable. RazorPageDemoYoutube …
1
vote
1 answer

How to mark a type as not null?

I want a generic method (or class) to behave something like this: val instanceS = MyCoolClass() instanceS.someMethod("some not null string") //compiles instanceS.someMethod(null) //doesn´t compile val instanceS2 =…
caeus
  • 3,084
  • 1
  • 22
  • 36
1
vote
0 answers

Validate a (JSON-deserialized) DTO with Nullable Reference Types

Using Non-Nullable Reference Types is great, but there seems to be no built-in support for enforcing Non-Nullability when compiling a class with nullable=enable and using something like XML or JSON deserialization. I know it's just compiler…
AyCe
  • 727
  • 2
  • 11
  • 30
1
vote
2 answers

Can I force an expression to be used with a non-nullable parameter?

I'm not sure how to explain this, so I'm gonna show you some code first and explain what it does. Extension Filter: receives a parameter expression and a special type of filter I've built for my application (IFilter). public static…
Ricardo
  • 172
  • 9